2013-02-09 49 views
0

说我想在脚本的父目录之一中找到文件。考虑这个文件树:使用PHP获取服务器上的文件的URL

Folder0 
    Folder1 
     needle.php 
     Folder2 
      Folder3 
       script.php 

我跑script.php,我需要找到哪些目录包含needle.php。我这样做:

while (!file_exists($nameFile)) { 
    chdir('..'); 
    } 
return getcwd(); 

这将返回“Folder0 \ Folder1 \”;

将此路径转换为此文件的URL的正确方法是什么?(例如`http://hostname.com/Folder0/Folder1/needle.php"

还是有一个更好的方式来获得一个服务器上的文件的URL?

+0

用[水珠()](http://php.net/manual/en/function.glob.php)实测值函数 – 2013-02-09 21:02:28

+0

此解决方案:HTTP: //stackoverflow.com/a/13616247/710356。它坚实吗? – sbichenko 2013-02-09 21:02:33

回答

0
while (!file_exists($nameFile)) { 
    chdir('..'); 
} 
return "http://".$_SERVER['HTTP_HOST'].str_replace('\\', '/', getcwd()); 
0

$ basePath =“http://hostname.com/”; $文件名= “needle.php”; $ folderPath =“Folder0 \ Folder1 \”; $ path = str_replace(“\”,“/”,$ folderPath); $ path = str_replace(“\”,“/”,$ folderPath); $ pathToFile = $ basePath。$ path。$ fileName;

echo $ pathToFile;

发挥它的功能!

卢西恩

2
getcwd() 

给您当前目录从服务器的根像/家/文森特/的public_html /文件夹你可以得到DOCUMENT_ROOT通过

$_SERVER['DOCUMENT_ROOT'];// /home/vincent/public_html 

必须从第一GETCWD的删除DOCUMENT_ROOT并与主机名称连接。这样

echo str_replace("\\",'/',"http://".$_SERVER['HTTP_HOST'].substr(getcwd(),strlen($_SERVER['DOCUMENT_ROOT']))); 

输出:

http://www.domain.com/folder 
相关问题