Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP根路径而不是localhost的相对路径_Php_Html_Localhost_Root - Fatal编程技术网

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

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

您好,我的本地主机上的目录路径有问题。我希望能够使用根路径与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 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++;
                                }
                        }

                                ?>