angelleye Posted September 5, 2010 Share Posted September 5, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/212560-rookie-question-regarding-include-files-with-php/ Share on other sites More sharing options...
PFMaBiSmAd Posted September 5, 2010 Share Posted September 5, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/212560-rookie-question-regarding-include-files-with-php/#findComment-1107364 Share on other sites More sharing options...
angelleye Posted September 5, 2010 Author Share Posted September 5, 2010 Ok, that makes sense. Can you provide any tip on how to get PHP to always refer to the site root no matter what server it's running from? Quote Link to comment https://forums.phpfreaks.com/topic/212560-rookie-question-regarding-include-files-with-php/#findComment-1107365 Share on other sites More sharing options...
PFMaBiSmAd Posted September 5, 2010 Share Posted September 5, 2010 I edited my post above with information about $_SERVER['DOCUMENT_ROOT'] that addresses that question. Quote Link to comment https://forums.phpfreaks.com/topic/212560-rookie-question-regarding-include-files-with-php/#findComment-1107366 Share on other sites More sharing options...
angelleye Posted September 5, 2010 Author Share Posted September 5, 2010 Seems to be working on the servers I'm playing with now. I don't know why I thought it seemed to take me to the actual document root instead of the site root in the past. I guess I'm good for now. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/212560-rookie-question-regarding-include-files-with-php/#findComment-1107369 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.