0

假设我想在脚本的父目录之一中找到一个文件。考虑这个文件树:

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?

4

4 回答 4

6
getcwd()

从服务器根目录为您提供当前目录,例如/home/vincent/public_html/folder您可以获得 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
于 2013-02-09T21:04:21.790 回答
0

您可以使用RecursiveDirectoryIterator 类

这是一个很好的教程,可以帮助你

我希望这能有所帮助,如果需要更多帮助,我们会在这里很高兴为您提供帮助 :)

于 2013-02-09T20:59:49.343 回答
0
while (!file_exists($nameFile)) {
    chdir('..');
}
return "http://".$_SERVER['HTTP_HOST'].str_replace('\\', '/', getcwd());
于 2013-02-09T21:01:27.343 回答
0

$basePath="http://hostname.com/"; $fileName="needle.php"; $folderPath="文件夹0\文件夹1\";

$path=str_replace("\","/",$folderPath); $pathToFile=$basePath.$path.$fileName;

回声 $pathToFile;

用它做一个功能!

卢西安

于 2013-02-09T21:02:06.133 回答