具有绝对路径的Php读取目录
我有一个从目录中读取mp3文件的php脚本:具有绝对路径的Php读取目录,php,directory,Php,Directory,我有一个从目录中读取mp3文件的php脚本: // Set your return content type header('Content-type: text/xml'); //directory to read $dir = ($_REQUEST['dir']); $sub_dirs = isBoolean(($_REQUEST['sub_dirs'])); if(file_exists($dir)==false){ //echo $_SERVER['DOCUMENT_ROOT
// Set your return content type
header('Content-type: text/xml');
//directory to read
$dir = ($_REQUEST['dir']);
$sub_dirs = isBoolean(($_REQUEST['sub_dirs']));
if(file_exists($dir)==false){
//echo $_SERVER['DOCUMENT_ROOT'];
//echo "current working directory is -> ". getcwd();
echo 'Directory \'', $dir, '\' not found!';
}else{
$temp = getFileList($dir, $sub_dirs);
echo json_encode($temp);
}
function isBoolean($value) {
if ($value && strtolower($value) !== "false") {
return true;
} else {
return false;
}
}
function getFileList($dir, $recurse){
$retval = array(); // array to hold return value
if(substr($dir, -1) != "/") // add trailing slash if missing
$dir .= "/"; // open pointer to directory and read list of files
$d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading");
while(false !== ($entry = $d->read())) { // skip hidden files
if($entry[0] == "." || $entry[0] == "..") continue;
if(is_dir("$dir$entry")) {
if($recurse && is_readable("$dir$entry/")) {
$retval = array_merge($retval, getFileList("$dir$entry/", true));
}
}else if(is_readable("$dir$entry")) {
$path_info = pathinfo("$dir$entry");
if(strtolower($path_info['extension'])=='mp3'){
$retval[] = array(
"path" => "$dir$entry",
"type" => $path_info['extension'],
"size" => filesize("$dir$entry"),
"lastmod" => filemtime("$dir$entry")
);
}
}
}
$d->close();
return $retval;
}
该文件与执行脚本的html页面位于同一目录中。例如,我传递相对路径“audio/mp3”或“../audio/mp3”,这一切都很好
是否可以使此读取目录具有绝对路径
例如,如果我传递此:,则执行脚本和php文件的html页面将放置在以下位置:
谢谢大家! Web根目录始终只是/。您不需要主机名或协议部分,root只能是服务器的根,而不是某个文件夹或文件 如果您需要一些路径,比如/testdesh/-有一些方法,但它与Webroot没有任何共同之处
如果您需要webroot的文件系统目录-它位于$\u SERVER['DOCUMENT\u ROOT']变量中。…您是否尝试过使用绝对路径运行它?请改用,也许允许他人读取您的文件系统不是一个好主意。