0

好的,这就是我所拥有的:在根目录中,有一个名为 Backgrounds 的文件夹,其中我有一组图像,其中一个图像是在我网站的每个页面上随机选择的。因此,在每个 .php 文件上,我需要更改这些图像的路径,即:

$dir = '../backgrounds/*';      
$array = array();
foreach(glob($dir) as $file) {
    $array[] = $file;
}
shuffle($array);
echo '<img src="'. $array[0] .'" alt="'. basename($array[0], '.jpg') .'"/>'

所以每次我深入文件柜时,我都需要修改 $dir 变量,有没有办法使该路径通用?

4

1 回答 1

0

我建议使用绝对路径:

// in some central config.php file
define('SITEROOT', '/path/to/webroot/');

include '../config.php';
foreach (glob(SITEROOT . 'backgrounds/*') as $file) ...
于 2011-02-25T06:09:40.430 回答