Jump to content

Rookie question regarding include files with PHP..???


angelleye

Recommended Posts

I'm a little confused with the use of absolute paths in PHP.  I've always used relative paths but it's becoming an issue with some of the apps I'm working with and absolute would solve the problem.

 

The thing is, I'm getting different results in different places and I'm a little confused.

 

With regular HTML I seem to be able to use / as the root of my site.  So if I do the following...

 

<img src="/images/name.jpg" />

 

That works fine no matter what page it's called from in the site and no matter what directory that page is in.  I need to be able to do that same thing with PHP.

 

Now, in my custom 404 page I have the following...

 

if($AddressURL == 'http://www.domain.com/blog/files/filename.zip')
{
header('HTTP/1.1 301 Moved Permanently');
header('Location: /download/filename.zip');
exit();
}

 

That actually works just fine. If I go to the /blog/files/filename.zip (which doesn't exist) in a web browser it winds up downloading the file at www.domain.com/download/filename.zip as expected. 

 

Now where I get confused is that include files don't seem to work like that with PHP.  If I do this...

 

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

 

That winds up looking for an /includes folder all the way back at the local server root, not just the web root.  so it's looking for /var/includes instead of /var/www/includes and of course that doesn't end up working.

 

So, how come the include file paths work differently than regular HTML paths or header redirects in PHP?  Is there any simple way I can always refer to the SITE root no matter what server it's running from?  I've tried playing with $_SERVER['DOCUMENT_ROOT'] but it only seems to return the root of the current document, not the root of the whole site.  So if I use it within a php file located in /var/www/includes/test.php it considers /includes the root which is not what I want.

 

Any information would be greatly appreciated.  Thanks!

 

Link to comment
Share on other sites

HTML paths are URL's, because it is the browser that is requesting the page being specified.

 

By default, include statements are file system paths, because it is the web server/php is that reading the file being specified.

 

URL's !== file system paths.

 

$_SERVER['DOCUMENT_ROOT'] will hold the file system path to your document root folder (assuming your web server is set up correctly.) You can prepend $_SERVER['DOCUMENT_ROOT'] to your file_path/file_name to supply an absolute file system path to an include statement.

 

Echo $_SERVER['DOCUMENT_ROOT'] to find out what it actually contains. It may or may not have the trailing slash /, depending on who setup your server.

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.