refering to the very top of the filesystem
my executed file is in
/homez.x/user/www/file.php
I need a f开发者_如何转开发unction to only get
/homez.x/user/
fair enough I can build a function stripping $_SERVER['document_root'] without the last folder, but need to know if is there only one method to achieve this
I'd merely do this:
$path = '/home/' . trim(`whoami`);
See also: shell_exec()
/homez.x/user/
isn't the root of the filesystem -- /
is. Are you looking for the home directory, or just the parent of the document root? (They may not be the same at all.)
The former can be determined using posix_getpwuid()
. The latter is simply dirname($_SERVER["DOCUMENT_ROOT"])
.
By hardcoding a directory structure you lessen the reusability of your code.
So what I would recommend is to add this directory-level to your include_path somewhere central - php.ini or by using set_include_path.
fopen, file & Co will look for the file in the "included paths" if you provide them with the propper flags.
精彩评论