Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从多个子文件夹中随机设置背景图像_Php_Css_Background Image - Fatal编程技术网

Php 从多个子文件夹中随机设置背景图像

Php 从多个子文件夹中随机设置背景图像,php,css,background-image,Php,Css,Background Image,我试图从几个子文件夹中随机设置一个网页的背景图像,但图像名称相同。 例如: 邮件文件夹可以是配置文件,然后是mlhrs,然后是RVS 在(RVS)文件夹中,您将有一个x编号或子文件夹,每个文件夹中都有profile.jpg图像 我的目标是,每次重新加载页面时,所有(RVS)子文件夹中的图像都会随机更改 我怎么做 我找到了这段代码,这几乎是我所需要的,但我不知道如何更改为最佳方式: 你能帮我吗? 非常感谢 <!DOCTYPE HTML> <html> <hea

我试图从几个子文件夹中随机设置一个网页的背景图像,但图像名称相同。 例如: 邮件文件夹可以是配置文件,然后是mlhrs,然后是RVS

在(RVS)文件夹中,您将有一个x编号或子文件夹,每个文件夹中都有profile.jpg图像

我的目标是,每次重新加载页面时,所有(RVS)子文件夹中的图像都会随机更改

我怎么做

我找到了这段代码,这几乎是我所需要的,但我不知道如何更改为最佳方式:

你能帮我吗? 非常感谢

<!DOCTYPE HTML>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="style.css">
        <title>404</title>
    </head>

    <body id="Background404">
        <p>404-Page not found. <a href="http://url.com">Home.</a></p>
    <?php
        $dir = './images';
        $fileNames = array();
        if(is_dir($dir)){
            $handle = opendir($dir);
            while(false !== ($file = readdir($handle))){
                if(is_file($dir.'/'.$file) && is_readable($dir.'/'.$file)){
                    $fileNames[] = $file;
                }
            }
            closedir($handle);
            $fileNames = array_reverse($fileNames);
            print_r($fileNames);
        }
        $totalLength = count($fileNames);
        $randInt = rand(0, $totalLength -1);
        $randFile = $fileNames[$randInt];
        echo $randFile;
        echo "<style> #Background404{background: url('./images/$randFile');}</style>";
    ?>

    </body>
</html>

404
找不到404页


随机使用洗牌

这里是解决方案代码

        $fileNames = shuffle($fileNames); // Shuffle All Array Files
        $randFile = end($fileNames); /*Get Last File Name Array (Last File Is Always Randomly, Becase We Did shuffle before get last element*/
        echo $randFile;
        echo "<style> #Background404{background: url('./images/$randFile');}</style>";````
$fileNames=shuffle($fileNames);//洗牌所有数组文件
$randFile=end($filename)/*获取最后一个文件名数组(最后一个文件总是随机的,因为我们在获取最后一个元素之前进行了洗牌*/
echo$randFile;
echo“#Background404{background:url('./images/$randFile');}”````

谢谢sasindu,但它不起作用。它没有从profiles/mlhrs/rvs上存在的所有子订单中只选择profille.jpg文件