简体   繁体   中英

PHP include path issues: “./” doesn't link where I expect it?

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!

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, eg 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, eg 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' include dirname(__FILE__) . '/foo/bar.php' . In practice, you will probably use a combination of both methods, ie, 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM