【问题标题】:PHP root path instead of relative path for localhostPHP根路径而不是localhost的相对路径
【发布时间】: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。它在文件系统级别运行,并且仅查看/使用文件系统路径。您必须提供网络空间地址和文件系统空间地址之间的转换。

标签: php html localhost root


【解决方案1】:

你可以试试这些:

$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 下可用。

【讨论】:

    【解决方案2】:

    这两个例子都不起作用,所以这是我现在想出的

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 2016-11-14
      • 1970-01-01
      • 2010-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多