PHP根路径而不是localhost的相对路径
您好,我的本地主机上的目录路径有问题。我希望能够使用根路径与php类似的html这是我所拥有的 HTML根路径:PHP根路径而不是localhost的相对路径,php,html,localhost,root,Php,Html,Localhost,Root,您好,我的本地主机上的目录路径有问题。我希望能够使用根路径与php类似的html这是我所拥有的 HTML根路径: /~Damian/home/ PHP 最后,这是我想做的,但似乎无法理解: // generates random images for slideshow $dir = "/~Damian/home/pics"; //set path to images $thumbDir = "/~Damian/home/pics"; //set path to image thumbnail
/~Damian/home/
PHP
最后,这是我想做的,但似乎无法理解:
// 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++;
}
你可以试试这些:
$dir = dirname(__FILE__)."/../pathToYourPicsDirectoryFromCurrentScriptLocation";
或
如果你把第二个例子放在一个配置文件中,你可以把它放在任何地方,你的pics目录在定义的变量PATH_pics下总是可用的。这两个例子都不起作用,所以这就是我现在想到的
<?php
if($frontPage=='yes'){
// 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++;
}
}
else{
// 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++;
}
}
?>
不可能。PHP不知道用于执行脚本的URL。它在文件系统级别运行,只查看/使用文件系统路径。您必须提供web空间地址和文件系统空间地址之间的转换。
define('PATH_ROOT',$_SERVER['DOCUMENT_ROOT']);
define('PATH_PICS',PATH_ROOT."/pics");
// in your script
$dir = PATH_PICS;
<?php
if($frontPage=='yes'){
// 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++;
}
}
else{
// 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++;
}
}
?>