问题
I did this:
echo dirname(____FILE____)
and it keeps showing as full absolute path as
C:/Program Files (x86)/VertrigoServ/www/dir/file.php
Why is that? I expect it to show
dir/file.php
I am using WAMP which is Apache on Windows.
BTW, calling $_SERVER['DOCUMENT_ROOT'] does the same thing - it shows full path.
回答1:
Try:
$webPath = str_replace($_SERVER['DOCUMENT_ROOT'], '', str_replace('\\', '/', __FILE__));
It'll take the absolute path to the file and remove the document root portion of it which should leave you with the path and file absolute from the document root.
回答2:
Although you don't state, if you're using wordpress I solved this issue by using:
file(bloginfo('home_url').'<path-to-file-without-leading-slash>')
来源:https://stackoverflow.com/questions/14823495/wamp-showing-absolute-path-when-echoing-dirname-file