PHP directory rules of thumb?
I'm trying to make sure my file paths are as robust as they can be, and everyone knows that hard-c开发者_开发技巧oding paths can be disastrous in a lot of cases. Are there any general rules of thumb regarding referencing paths? Mostly concerning referencing above $_SERVER['DOCUMENT_ROOT'].
I've been doing ../../(x100000), but it looks messy and was hoping there was a cleaner way.
Thanks
To get the current working directory: getcwd();
To get the currect directory name: basename(getcwd());
To get the current directory of the running script I usually do:
str_replace('//', '/', str_replace('\\', '/', dirname(__FILE__) . '/'));
This is a little bit hacky but it's reliable AFAIK.
But I think it will not work with Windows UNC paths (if you use them)...
Take a lesson from the Codeigniter framework (which handles relative paths notoriously well).
Take a look at the code in the index file:
https://github.com/philsturgeon/codeigniter-reactor/blob/master/index.php
You can see that there are a variety of ways to handle this scenario (some better than others), but that file will show you the best practices.
精彩评论