Jump to content

Include paths and doc_root?


FalcorTheDog

Recommended Posts

My server code is littered with includes/requires of the form: include('/includes/somefile.php');

 

It works fine on the server, since my "includes" folder is in the root directory on the web server.  However, now I have a test server setup on my local machine, and am trying to test the same code without changing all of the includes, but the backslash ('/') is pointing to the root directory on my computer instead of the root folder of my local server.  It looked as if editing the doc_root in my php.ini file would solve this problem, but I haven't had any luck.  Any ideas?

 

(I'm running MAMP locally on OS X 10.5)

 

Thanks in advance!

Link to comment
Share on other sites

This is why you should always use specific paths.

 

As ignace is saying, it would work with the ../ and ./ instead of your / (Which by default refers to the root of the server)

 

Using / makes it very difficult to move your site to another server in the future, since it may not be possible to place the site in the root folder.

 

You should consider remaking your system to use specific paths :)

Link to comment
Share on other sites

Yeah, it appears to work if I change each include to use './'

 

but so does this mean I'm SOL and have to rework all my code?

 

Using paths relative to the root are nice so that you can move files around in the directory structure on your server without worrying about breaking things, but now I wonder if it's worth it.

Link to comment
Share on other sites

but so does this mean I'm SOL and have to rework all my code?

 

No, your code and SQL will still work ;) you only need to adjust the paths.

 

Using paths relative to the root are nice so that you can move files around in the directory structure on your server without worrying about breaking things, but now I wonder if it's worth it.

 

That's not true. If you have a file located in /includes/somefile.php and now I move somefile.php to /somedir. Then how will this code still works?

 

require_once('/includes/somefile.php');

 

Consider to always use absolute paths using either realpath() or by defining directory constants:

defined('__DIR__') or define('__DIR__', dirname(__FILE__)); //__DIR__ exists as of PHP 5.3.0
define('DOCUMENT_ROOT', __DIR__);

require_once(DOCUMENT_ROOT . '/includes/somefile.php');

 

Personally I prefer realpath() as it modifies the directory separator to the underlying operating systems' directory separator.

Link to comment
Share on other sites

That's not true. If you have a file located in /includes/somefile.php and now I move somefile.php to /somedir. Then how will this code still works?

 

You're correct.  What I meant was: if I wanted to move the file that was actually doing the "including" to another directory, I could do so without changing the relative path of every include within that file.  I think the best bet in the future is to have a constant defined with the root path and just concatenate it with the beginning of every include path.

 

But I'm still really baffled that I can't just tell PHP which directory I want to function as the root...

Link to comment
Share on other sites

But I'm still really baffled that I can't just tell PHP which directory I want to function as the root...

 

You can, however only in safe_mode (or by using chdir()) and if you have php.ini access as doc_root is PHP_INI_SYSTEM. However /includes/file.php will still throw errors i guess as you then are trying to access files outside the doc_root. Just remember to always start without a / (require_once('includes/file.php') which assumes the current work directory (getcwd()).

 

Maybe user_dir can help: http://www.php.net/manual/en/ini.core.php#ini.user-dir

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.