Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.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获取图像的通用SRC路径_Php_Image_Path_Src - Fatal编程技术网

PHP获取图像的通用SRC路径

PHP获取图像的通用SRC路径,php,image,path,src,Php,Image,Path,Src,好的,这就是我所拥有的: 在根目录中,有一个名为Backgrounds的文件夹,其中我有一组图像,在我的站点的每个页面上随机选择一个图像。 因此,在每个.php文件上,我需要更改这些图像的路径,即: $dir = '../backgrounds/*'; $array = array(); foreach(glob($dir) as $file) { $array[] = $file; } shuffle($array); echo '<img src="'. $arra

好的,这就是我所拥有的: 在根目录中,有一个名为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='../backgrounds/*';
$array=array();
foreach(全局($dir)作为$file){
$array[]=$file;
}
洗牌($数组);
回声“

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

我建议使用绝对路径,然后:

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

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

什么意思?你能澄清你的问题吗