greg Posted March 2, 2012 Share Posted March 2, 2012 How to include root files from subdirectory in php? I have tried include '../configure.php'; but got error. Also tried include($DOCUMENT_ROOT."../configure.php"); but same result. For example, I have "root/admin" directory, and can't connect (from "/admin" dir) to the file configure.php which is in root. There are also other files to include. Quote Link to comment https://forums.phpfreaks.com/topic/258125-include-root-file-from-subdirectory/ Share on other sites More sharing options...
Psycho Posted March 2, 2012 Share Posted March 2, 2012 Not sure what your problem is. Using '../' should let you access files that are one level up from the file being executed. The error you received should give you the FULL path to where the server is looking for the file. What WAS the error? Assuming the below diagram is the structure in question, using '../configure.php' in the index.php file should work. [ROOT] | |-- configure.php | |-[admin] | |-- index.php Quote Link to comment https://forums.phpfreaks.com/topic/258125-include-root-file-from-subdirectory/#findComment-1323174 Share on other sites More sharing options...
requinix Posted March 2, 2012 Share Posted March 2, 2012 Using '../' should let you access files that are one level up from the file being executed. From the first file that was executed (generally). Subtle difference. Moral is to always use absolute paths whenever possible, either with the DOCUMENT_ROOT or the __FILE/DIR__ constants. require_once($_SERVER["DOCUMENT_ROOT"] . "/configure.php"); Quote Link to comment https://forums.phpfreaks.com/topic/258125-include-root-file-from-subdirectory/#findComment-1323185 Share on other sites More sharing options...
Psycho Posted March 2, 2012 Share Posted March 2, 2012 Using '../' should let you access files that are one level up from the file being executed. From the first file that was executed (generally). Subtle difference. You're right. I considered that for 1/2 a second, but then second guessed myself and thought it didn't apply to this situation. But, now that I consider it, I don't know what I was thinking (or not thinking). That could very well be the problem. Quote Link to comment https://forums.phpfreaks.com/topic/258125-include-root-file-from-subdirectory/#findComment-1323227 Share on other sites More sharing options...
greg Posted March 2, 2012 Author Share Posted March 2, 2012 Thank you. I get it working with this: require_once($_SERVER["DOCUMENT_ROOT"] . "/configure.php"); The best! Quote Link to comment https://forums.phpfreaks.com/topic/258125-include-root-file-from-subdirectory/#findComment-1323266 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.