...

View Full Version : How Does PHP Determine Require File Relative Path?



dealmaker
12-11-2005, 04:45 AM
Hi,
I have a require('file.php'); how does php determine the relative path of file.php? Is it the same path of the file that has the require() or is it the root of the web server like /var/www/html?

Many thanks.

Velox Letum
12-11-2005, 04:56 AM
Relative path means a path relative to the file. So if in example.php you put require('file.php');, it would include file.php from the same directory as example.php. 'foo/file.php' would be file.php in the foo directory inside the example.php's directory. '../file.php' would be file.php in the parent directory to example.php's directory.

Example:

/home/user/public_html/index.php includes 'includes/header.php' PHP would be including /home/user/public_html/includes/header.php

Or if /home/user/public_html/index.php includes '../header.php' then PHP would be including /home/user/header.php.

Of course, PHP doesn't write it out like this. It executes a filesystem call and it determines what file ../header.php is.

ralph l mayo
12-11-2005, 06:40 AM
Note that the relative starting point is always from the file being executed. Ie., if you have:

/include.php
/myapp/index.php
/myapp/otherinclude.php

if index.php (the file the users requests) includes '../include.php', include.php must include otherinclude as 'otherinclude.php' and not 'myapp/otherinclude.php'

For this reason if you have includes spanning several directories or includes that are used in different contexts, it's often easier to define a root path and include absolutely.

Velox Letum
12-11-2005, 07:22 AM
I myself move all my critical code to a web-inaccessable directory, then in their stead have files which require the files they need to function. More for security than anything, as once when I recompiled Apache I forgot to include PHP in there and all my code was displayed for the world to see. This way, all people see is a few requires and echos, not the actual code were it to happen again.

dealmaker
12-11-2005, 07:58 AM
I don't quite understand what you said. Did you mean to move all files besides ones that's actually on the url to outside of the web directory? If so, will I get permission error when I require them in the php files? How exactly do you do this?

I heard that people can view php script if they pull the network cable during webpage loading.


I myself move all my critical code to a web-inaccessable directory, then in their stead have files which require the files they need to function. More for security than anything, as once when I recompiled Apache I forgot to include PHP in there and all my code was displayed for the world to see. This way, all people see is a few requires and echos, not the actual code were it to happen again.

Velox Letum
12-11-2005, 08:51 AM
No, PHP includes files through the filesystem, not through the web, which is why it works. And whoever told you that stopping a page in transaction would allow you to see the code doesn't know what they're talking about. All PHP is executed before the page is even sent to the client.

You don't need to (or probably want to) move your critical PHP files to outside of the webroot, it's just my inherent paranoia. The only reason I do it really is because I'm always recompiling Apache and PHP to test new extensions and such. Ah, the perks of your own server...

firepages
12-11-2005, 12:27 PM
Note that PHP also looks in its include_path as set in the php.ini (or in httpd conf or can be changed at runtime with ini_set())

so with include('file.php') php first looks in the current working directroy , if it does not find it there is checks for the file in the include_path (first in, best dressed if multiple paths)

I tend to use absolute paths anyway so not an issue for me.

rmedek
12-11-2005, 08:26 PM
I tend to use absolute paths anyway so not an issue for me.
Forgive my noobishness, but does that mean you are using "/path/from/server/to/domain/file.php" for every include? I use includes a lot and was hoping there was some way to set the domain as the root rather than the server, as in HTML.

I know, you could use "<?php include($DOCUMENT_ROOT . "/includes/file.php"); ?>" but it's not like anyone's saving any typing. :)

dealmaker
12-11-2005, 10:31 PM
that's exactly my problem. I have 'include' directory for classes. Those files have the same name as the ones that 'require' the files inside 'include'. There are also templates directory. So there can be a chain of files in different directories chained by the 'require' functions. If I use relative path, it will certainly create problems when PHP tries to find the files. I am trying to solve that without using absolute path. How?



For this reason if you have includes spanning several directories or includes that are used in different contexts, it's often easier to define a root path and include absolutely.

Velox Letum
12-11-2005, 10:36 PM
Well let me answer a question there. Say you have a structure like this:

index.php
-includes
includes/blah.php
which includes
includes/blah2.php

If index.php includes blah.php, then you include blah2.php from blah.php you need to include 'includes/blah2.php', as it determines the relative path from where it was included.

dealmaker
12-11-2005, 10:48 PM
yes, I understood this part. But most likely, I have situation when there is another file index2.php in a different directory than index.php and it include includes/blah.php as well.
Now, PHP won't find blah2.php from index2.php.


Well let me answer a question there. Say you have a structure like this:

index.php
-includes
includes/blah.php
which includes
includes/blah2.php

If index.php includes blah.php, then you include blah2.php from blah.php you need to include 'includes/blah2.php', as it determines the relative path from where it was included.

Velox Letum
12-11-2005, 10:54 PM
You could just use absolute paths, ie:

/home/user/public_html/includes/blah2.php

Then you wouldn't have to worry about it.

firepages
12-12-2005, 03:12 AM
Forgive my noobishness, but does that mean you are using "/path/from/server/to/domain/file.php" for every include? I use includes a lot and was hoping there was some way to set the domain as the root rather than the server, as in HTML.

I know, you could use "<?php include($DOCUMENT_ROOT . "/includes/file.php"); ?>" but it's not like anyone's saving any typing. :)


well nearly , I use
<?php include_once FP_FILE_ROOT.'/path/to/file';?>
where FP_FILE_ROOT has been previously defined as /home/httpd/etc in a globally included file (which sadly has to be included via a relative path else there is no point ;))

it saves little typing today you are correct , but if you move to a different host or change path then you have only to edit the 1 file that defines that path.

also I develop locally on windows and test on my local backup linux box & a simple conditional lets the code run on all 3 without any changes at all.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum