Eric
Eric

Reputation: 2996

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\Apache2.2\htdocs\

I think that this is pretty straightforward but if there is something you don't understand just comment

Upvotes: 3

Views: 649

Answers (3)

Powerlord
Powerlord

Reputation: 88796

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']

Upvotes: 1

Pascal MARTIN
Pascal MARTIN

Reputation: 400972

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__);

Upvotes: 3

Amy B
Amy B

Reputation: 17977

In PHP 5.3: __DIR__

In lower version: dirname(__FILE__)

Upvotes: 11

Related Questions