[英]PHP root path instead of relative path for 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++;
}
您可以嘗試以下方法:
$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下可用。
這兩個示例都不起作用,所以這就是我現在想出的
<?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++;
}
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.