3

配列内のフォルダーからファイルを取得するのに役立ちます。arrayフォルダーからすべての jpg ファイル名を取得しようとしていますimages。その後、randCSSの背景をランダムに変更するために使用します。

arry 内のフォルダーからの JPG ファイル。

$images=array("image1.jpg", "image2.jpg");

次に、を使用しrandて画像をランダムにロードします

echo '<style>body{background:url('.$images[array_rand($images)].')no-repeat;';
4

2 に答える 2

4

ディレクトリをscandirに渡して、そのディレクトリ内のすべてのファイルの配列を取得します。おそらく、array_filterを使用して、ファイル拡張子で画像以外を除外します。

    $files = scandir( '/image/path' );

    function images_only( $file )
    {
      return preg_match( '/\.(gif|jpg|png)$/i', $file );
    }

    $files = array_filter( $files, 'images_only' );

$filesには、画像パスからの画像のみが含まれるようになりました。

于 2012-08-26T22:26:13.653 に答える
1

それをグロブする

array_rand 修正で編集

$images = glob("images/*.jpg");
// may want to verify that the $images array has elements before echoing
echo '<style>body{background:url('.$images[array_rand($images)].') no-repeat;';
于 2012-08-26T14:59:41.280 に答える