开发者

PHP Getting generic SRC path for images

Ok, so this is what I have: On the root directory, there is a folder with the name Backgrounds where I have a set of images, one of those images is selected randomly on each page of my site. So on each .php file I need to change the path to those images, ie:

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

So each time I go deeper into the file cabinet I need to modyfy the $dir variable, isn't there a way to make that path generic?


I'd advise to use absolute paths then:

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜