How do I detect the directory a php file is in
I have a PHP file that I need it to detect it's directory it's in. In my case I want it to return C:\Program Files\Apache Software Foundation\开发者_StackOverflow中文版Apache2.2\htdocs\
I think that this is pretty straightforward but if there is something you don't understand just comment
In PHP 5.3: __DIR__
In lower version: dirname(__FILE__)
The magic-constant __FILE__
contains the full path to the file in which you write it.
The dirname
function returns the path to the directory corresponding to a file.
So, in your case, to get the path to the directory containing your file, you can use :
echo dirname(__FILE__);
In addition to the directory the file is in, you may find the directory that corresponds to the web server's /
URI (http://www.example.com/
URL) useful. That's stored in $_SERVER['DOCUMENT_ROOT']
精彩评论