首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有绝对路径的Php读取目录

问具有绝对路径的Php读取目录
EN

Stack Overflow用户
提问于 2013-09-28 17:39:41
回答 1查看 693关注 0票数 0

我有一个从目录中读取mp3文件的php脚本:

代码语言:javascript
复制
// 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' or '../audio/mp3',它工作得很好。

有没有可能使这个读目录具有绝对路径?

例如,如果我传递这个:http://www.mydomain.com/someFolder/audio/mp3,执行脚本和php文件的html页面将被放在这个位置:http://www.mydomain.com/someFolder/。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2013-09-28 17:59:00

Web根目录始终只是/。您永远不需要主机名或协议部分,并且root可以只是服务器的根目录,而不是某个文件夹或文件。

如果你需要一些路径,比如/testthesis/ -是有办法的,但是它和web root没有什么共同之处。

如果您需要webroot的文件系统目录-它位于$_SERVER‘’DOCUMENT_ROOT‘变量中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19065500

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档