【问题标题】:PHP Getting generic SRC path for imagesPHP获取图像的通用SRC路径
【发布时间】:2011-07-04 02:31:20
【问题描述】:

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

【问题讨论】:

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

标签: php image path src


【解决方案1】:

我建议使用绝对路径:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    相关资源
    最近更新 更多