【发布时间】:2014-08-15 14:33:53
【问题描述】:
您好,我的本地主机上的目录路径有问题。我希望能够使用类似于 html 的 php 根路径,这就是我所拥有的
HTML 根路径:
/~Damian/home/
PHP
// generates random images for slideshow
$dir = "../pics"; //set path to images
$thumbDir = "../pics"; //set path to image thumbnails
$numberToDisplay = 17; //number of images to display
if ($handle = opendir($thumbDir)) {
while(false !== ($file = readdir($handle))){
if (!preg_match('/^\.+$/', $file) and
preg_match('/(\.jpg|\.gif|\.png|\.JPG|\.jpeg|\.JPEG)$/', $file)){
$files[] = $file;
}
}
closedir($handle);
}
$i = 0;
$images = array_rand(array_flip($files), $numberToDisplay);
while ($i < $numberToDisplay){
echo "<div><img src='$thumbDir/$images[$i]' width='320' height='240' alt='random image' /></div>";
$i++;
}
最后这是我想做但似乎无法弄清楚的:
// generates random images for slideshow
$dir = "/~Damian/home/pics"; //set path to images
$thumbDir = "/~Damian/home/pics"; //set path to image thumbnails
$numberToDisplay = 17; //number of images to display
if ($handle = opendir($thumbDir)) {
while(false !== ($file = readdir($handle))){
if (!preg_match('/^\.+$/', $file) and
preg_match('/(\.jpg|\.gif|\.png|\.JPG|\.jpeg|\.JPEG)$/', $file)){
$files[] = $file;
}
}
closedir($handle);
}
$i = 0;
$images = array_rand(array_flip($files), $numberToDisplay);
while ($i < $numberToDisplay){
echo "<div><img src='$thumbDir/$images[$i]' width='320' height='240' alt='random image' /></div>";
$i++;
}
【问题讨论】:
-
不可能。 PHP 不知道用于执行脚本的 URL。它在文件系统级别运行,并且仅查看/使用文件系统路径。您必须提供网络空间地址和文件系统空间地址之间的转换。