PHP根路径而不是localhost的相对路径

时间:2014-08-15 14:33:54

标签: php html localhost root

您好我的localhost上的目录路径出现问题。我希望能够使用类似于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++;
}

2 个答案:

答案 0 :(得分:0)

你可以试试这些:

$dir = dirname(__FILE__)."/../pathToYourPicsDirectoryFromCurrentScriptLocation";

或者

define('PATH_ROOT',$_SERVER['DOCUMENT_ROOT']);
define('PATH_PICS',PATH_ROOT."/pics");

// in your script
$dir = PATH_PICS;

如果您将第二个示例放在可以包含在任何位置的配置文件中,那么您的pics目录将始终在定义的变量PATH_PICS下可用。

答案 1 :(得分:0)

这两个例子都没有用,所以这就是我现在想出来的

<?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++;
                                }
                        }

                                ?>