PHP获取图像的通用SRC路径

时间:2011-02-25 06:04:34

标签: php image path src

好的,这就是我所拥有的: 在根目录中,有一个名为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变量,是不是有办法使该路径通用?

1 个答案:

答案 0 :(得分:0)

我建议使用绝对路径:

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

include '../config.php';
foreach (glob(SITEROOT . 'backgrounds/*') as $file) ...
相关问题