Dealing with include and complex directory hierarchy in PHP

hilllouse

New Member
I have to deal with complex directories hierarchy, and I am facing the common trouble of include path with PHP.I have searched the web but I haven't found anything that fit my needs.For instance, I was using a simple directory hierarchy that never fail: no php script in the site root, only one level of subdirectory, all php script in this sublevel. To include a php file, I was simply using relative path, always starting with '../' just like in this example:\[code\]include( '../my_subdirectory/my_script.php' ) ;\[/code\]This way, I can be sure to locate the file I want... But there is some drawback:
  • I can't have more than one level of subdirectory (reason: when a file include a file that include another file, the path used to include the third file is not relative to the path of second file file, but relative to the path of the very first file).
  • Coming from a C++ background (using handmade makefile), I have always thought it was a dirty way to do it
So I want a way to include file directly from the site root (not the $_SERVER['DOCUMENT_ROOT'] because I may have independant website into subdirectory of this document_root). I want it to be:
  • centralized in only one file
  • portable from a server to another without any change (if possible)
  • keep php's include simple and elegant, no complex string concat, this should work this way: "include('directory_a/directory_b/my_php_script.php')"
Using a .htaccess that contains:\[code\]php_value include_path "/var/www/my_website/" \[/code\]... do it well except that the path is hardcoded into the .htaccess, annoying for some reason: in my case, I have prod, dev and testing version of the website, and the .htaccess is versionned (it contains many others things). If possible, I want an .htaccess that work everywhere. Something that set the include_path to the path of this current .htaccess would be fine.So... What is the best practice, dealing with include() and complex directory hierarchy in PHP ?
 
Back
Top