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) ...
精彩评论