what is the real working directory?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PaulMM
Forum Newbie
Posts: 15
Joined: Wed Oct 01, 2008 8:21 am

what is the real working directory?

Post by PaulMM »

I have a file myscript.php which works with lots of txt files by reading and modifing them.
My script is called by lots of other files from parent folder like:
c:/unknown_installation_path/parentfile.php -- includes myscript.php (i'm not allowed to modify them because of the parent script architecture)
c:/unknown_installation_path/project/my/myscript.php -- I can modify it.

sometimes surfers navigate to parent files (which include myscript.php) or sometimes they navigate to myscript.php directly.
Location of myscript is different on each server machine because of the installation path.

I work with myscript.php only and I need to know what is its current directory in both cases: when is it called directly and when it is called via an include from parent folder.
How can I do that? Obviously, I cannot use standard great function getcwd() because it returns different paths if my file is called via includes from parent folders.

Is there any permanent solution to it? Thanks.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: what is the real working directory?

Post by Eric! »

I think you're looking for __FILE__ in the case of the include and $_SERVER['SCRIPT_FILENAME'] in the case of being called.

Here's an example with information about different server configurations:
http://www.bin-co.com/php/articles/curr ... e_path.php
Post Reply