PHP 包含路径问题:“./”没有链接到我期望的地方?

发布于 2024-11-27 08:32:59 字数 1818 浏览 2 评论 0原文

我正在从头开始开发一个 php MVC 框架,并且遇到了一个关于默认 WAMP 部署的相对路径的有趣问题...当我开始在框架内编写 include 和 require 时,问题就出现了...

我的框架条目point 位于文档根目录深处的一个文件夹:

http://localhost/platform/index.php < - 框架入口点;物理路径是 c:/wamp/www/platform/

当我从框架中的任何文件运行以下目录遍历代码时...

$handle = opendir('./');
while ($file = readdir($handle))
{    
    echo $file."<br/>";   
}

...我得到相同的结果:

.
..
ABOUT_APACHE.txt
bin
cgi-bin
CHANGES.txt
conf
error
htdocs
icons
include
INSTALL.txt
lib
LICENSE.txt
logs
manual
modules
NOTICE.txt
OPENSSL-NEWS.txt
OPENSSL-README.txt
README-win32.txt
README.txt
wampserver.conf

这是 c:/ 文件夹的内容wamp/bin/Apache2.2.17/

转储 ini_get('include_path') ".;c:/php/pear" ...这很奇怪,因为它是默认的 WAMP 安装,它将 php 放在c:/wamp/bin/php/php5.3.4/,据我所知,没有将 PEAR 放在任何地方(我在 php5.3.4 文件夹中没有看到它!)...

首先问题:为什么 webroot 中任何特定文件中的“./”都不是指调用它的文件所在的目录?第一个问题,第二部分:为什么它总是引用 wamp 部署的 apache 文件夹的根目录,而不管调用文件的位置如何?

将文件夹的内容转储到“/”显示:

$INPLACE.~TR
$RECYCLE.BIN
$WINDOWS.~Q
boot
bootmgr
BOOTSECT.BAK
cert.pfx
Config.Msi
Documents and Settings
DVDPATH.TXT
hiberfil.sys
HP
msdia80.dll
MSOCache
pagefile.sys
PerfLogs
Program Files
Program Files (x86)
ProgramData
Recovery
SWSetup
System Volume Information
System.sav
updatedatfix.log
Users
wamp
Windows

第二个问题 - 为什么从我的脚本打开时路径“/”引用 C:\ 而不是文档根目录(这将是 C: \wamp\www)

最后一个问题 - 如何配置 PHP 以确保我相信的是这些情况下的预期路径(/ => 文档根目录,./ => 引用的文件夹脚本)实际上是这些参考文献的路径?

我完全做错了吗?有一次,我认为相对路径是引用 CALLED 脚本,或者是入口点的文件夹,在本例中为 c:/wamp/www/platform/...无论哪个文件进行引用,但 php.ini 是相对路径。 net 文档似乎另有说明。 PHP.net 表示,无论 FILE 所在的文件夹使用相对路径(带前导句点)执行函数,都应该是相对路径的起始位置。我的配置似乎并非如此...

感谢您的宝贵时间!

I am developing a php MVC framework from scratch and have run into an interesting problem with the relative pathing of a default WAMP deployment... the problem arose when I was beginning to write includes and requires from within the framework...

My framework entry point lives one folder deep in the document root:

http://localhost/platform/index.php <- framework entry point; the physical path being c:/wamp/www/platform/

When I run the following directory traversal code from ANY file in the framework...

$handle = opendir('./');
while ($file = readdir($handle))
{    
    echo $file."<br/>";   
}

...I get the SAME RESULT:

.
..
ABOUT_APACHE.txt
bin
cgi-bin
CHANGES.txt
conf
error
htdocs
icons
include
INSTALL.txt
lib
LICENSE.txt
logs
manual
modules
NOTICE.txt
OPENSSL-NEWS.txt
OPENSSL-README.txt
README-win32.txt
README.txt
wampserver.conf

That is the contents of the folder at c:/wamp/bin/Apache2.2.17/

dumping the ini_get('include_path') ".;c:/php/pear" ...This is curious because it's the default WAMP install, which puts php at c:/wamp/bin/php/php5.3.4/, and as far as I can tell, doesn't put PEAR anywhere (I don't see it in the php5.3.4 folder!)...

First question: Why isn't "./" from within any particular file in the webroot referring to the directory in which the file from which it is called sits? First question, second part: why is it always referring to the root of the wamp-deployed apache folder, regardless of calling file's location?

Dumping the contents of the folder at "/" reveals:

$INPLACE.~TR
$RECYCLE.BIN
$WINDOWS.~Q
boot
bootmgr
BOOTSECT.BAK
cert.pfx
Config.Msi
Documents and Settings
DVDPATH.TXT
hiberfil.sys
HP
msdia80.dll
MSOCache
pagefile.sys
PerfLogs
Program Files
Program Files (x86)
ProgramData
Recovery
SWSetup
System Volume Information
System.sav
updatedatfix.log
Users
wamp
Windows

Second Question - why does the path "/" when opened from my script refer to C:\ and not the document root (which would be C:\wamp\www)

Last Question - How do I configure PHP to ensure that what I BELIEVE is the expected pathing in those situations (/ => document root, ./ => folder of the referencing script) actually IS the path for those references?

Am I totally doing this wrong? At one time I thought that the relative pathing was in reference to the CALLED script, or the entry point's folder, c:/wamp/www/platform/ in this case... Regardless of what file is making the reference, but php.net documentation seems to say otherwise. PHP.net says that whatever folder the FILE is in that is executing the function using a relative path (with leading period) should be the starting location of the relative path. Neither seems to be the case in my configuration...

Thanks for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抱着落日 2024-12-04 08:32:59

第一个问题:相对包含路径是相对于当前工作目录解释的;当通过 apache + mod_php 运行时,这通常是直接从 apache 调用的顶级脚本的位置。这是一个有点不幸的设计决定,由于 PHP 的流行,该决定无法撤销。

第二个问题: PHP 最初是为类 Unix 系统编写的,其中 / 表示绝对文件系统根。 Windows 没有这样的东西,因此 PHP 使用次佳的东西,即主文件系统上的顶级目录,通常是 C:\

第三个问题:这里有两个解决方案。第一个解决方案:在自动前置脚本中设置正确的 include_path,并仅使用相对名称,例如,当您有 C:\www\myproject\foo\bar.php 时>,将 C:\www\myproject\foo 添加到您的 include_path,然后include 'foo.php'。第二种解决方案:使用 dirname() 函数和 __FILE__ 超全局函数的组合,使您的包含显式地相对于当前文件,例如,当您想要包含 时C:\www\myproject\foo\bar.php 来自 C:\www\myproject\baz.php,使用类似 include dirname(__FILE__) 的内容。 '/foo/bar.php'。实际上,您可能会结合使用这两种方法,即在自动前置脚本中使用dirname(__FILE__) 技巧来设置相对于自动前置脚本本身的包含路径。

如果您使用 PHP 的 OOP 功能,您还需要设置一个类自动加载器,这样您就不必显式包含您使用的所有类的定义。

哦,您可能想阅读文档:http://www. php.net/manual/en/function.include.php

First question: Relative include paths are interpreted relative to the current working directory; when run through apache + mod_php, this is usually the location of the top-level script that gets called directly from apache. This is a somewhat unfortunate design decision which, due to PHP's popularity, cannot be undone.

Second question: PHP was originally written for Unix-like systems, where / indicates the absolute filesystem root. Windows does not have such a thing, so instead, PHP uses the next best thing, the top-level directory on the principal filesystem, which is usually C:\.

Third question: Two solutions here. First solution: set up a proper include_path in your auto-prepend script, and use only relative names, e.g. when you have C:\www\myproject\foo\bar.php, add C:\www\myproject\foo to your include_path, and just include 'foo.php'. Second solution: use a combination of the dirname() function and the __FILE__ superglobal to make your includes explicitly relative to the current file, e.g. when you want to include C:\www\myproject\foo\bar.php from C:\www\myproject\baz.php, use something like include dirname(__FILE__) . '/foo/bar.php'. In practice, you will probably use a combination of both methods, i.e., use the dirname(__FILE__) trick in the auto-prepend script to set the include path relative to the auto-prepend script itself.

If you use PHP's OOP features, you will also want to set up a class autoloader, so that you don't have to explicitly include the definitions of all the classes you use.

Oh, and you probably want to read up on the documentation: http://www.php.net/manual/en/function.include.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文