Php以数组形式从文件夹中获取jpg文件

Php以数组形式从文件夹中获取jpg文件,php,random,Php,Random,帮助我从阵列中的文件夹获取文件。 我正试图从文件夹图像中获取数组中的所有jpg文件名。 然后使用rand随机更改css背景 arry文件夹中的JPG文件 $images=array("image1.jpg", "image2.jpg"); 然后使用rand随机加载图像 echo '<style>body{background:url('.$images[array_rand($images)].')no-repeat;'; echo'body{background:url('.$

帮助我从阵列中的文件夹获取文件。 我正试图从文件夹
图像
中获取
数组中的所有jpg文件名。
然后使用
rand
随机更改css背景

arry文件夹中的JPG文件

$images=array("image1.jpg", "image2.jpg");
然后使用
rand
随机加载图像

echo '<style>body{background:url('.$images[array_rand($images)].')no-repeat;';
echo'body{background:url('.$images[array_rand($images)]。)不重复;';
it

使用数组进行编辑

$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;';
$images=glob(“images/*.jpg”);
//在回显之前,可能需要验证$images数组是否包含元素
echo'body{background:url('.$images[array_rand($images)]。)不重复;';
将目录传递给,以获取该目录中所有文件的数组。 可能使用then按文件扩展名过滤掉任何非图像

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

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

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

$files现在应该只包含图像路径中的图像。

Sooo…问题是什么?你有没有尝试过的不充分的东西?我不知道如何从gmail.com上williamcomartin编写的数组中的文件夹中获取文件-查找函数:
echo'body{background:url('.array\u rand(glob(“images/*.jpg”)。)不要重复;“;
@AaronW.-我猜这可能是一个很好的答案,尽管还有其他方法。路径正确吗?请尝试调试以确保正确填充
array_rand()
仅返回随机键。