By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,831 Members | 2,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,831 IT Pros & Developers. It's quick & easy.

Determine folder a script is running in.

P: n/a
What is the most reliable method to determine the folder a script is
running in?

I was looking at the globals in $_SERVER but all the variables also
listed the actual file the script was running in.

Thanks
Scotty
Feb 22 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Scott wrote:
What is the most reliable method to determine the folder a script is
running in?

I was looking at the globals in $_SERVER but all the variables also
listed the actual file the script was running in.

Thanks
Scotty
How about dirname($_SERVER['SCRIPT_FILENAME'])?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Feb 22 '08 #2

P: n/a
Jerry Stuckle wrote:
Scott wrote:
>What is the most reliable method to determine the folder a script is
running in?

I was looking at the globals in $_SERVER but all the variables also
listed the actual file the script was running in.

Thanks
Scotty

How about dirname($_SERVER['SCRIPT_FILENAME'])?
That worked perfectly, thanks Jerry.
Feb 22 '08 #3

P: n/a
Greetings, Jerry Stuckle.
In reply to Your message dated Friday, February 22, 2008, 05:30:08,
Scott wrote:
>What is the most reliable method to determine the folder a script is
running in?

I was looking at the globals in $_SERVER but all the variables also
listed the actual file the script was running in.

Thanks
Scotty
How about dirname($_SERVER['SCRIPT_FILENAME'])?
This is in no way close to trusted information about actual directory script
running in.

You can either chdir() inside script or have it started from other directory
by hands or other program (cron as one possible example)
Just checked: wrote a simple script in my home directory

<?php

phpinfo();
$s = dirname($_SERVER['SCRIPT_FILENAME']);

file_put_contents('test.$$$', $s);

?>

and have it started from the C:\ by calling it with full path

Then, contents of C:\test.$$$ was
C:\Profiles\<Username>
which is not an actual path, because CWD was C:\ where test.$$$ was
stored.

Solution is
$cwd = realpath('.');
It working fine under Windows. Hope it is true for other systems.
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Feb 22 '08 #4

P: n/a
AnrDaemon a écrit :
Greetings, Jerry Stuckle.
In reply to Your message dated Friday, February 22, 2008, 05:30:08,
>Scott wrote:
>>What is the most reliable method to determine the folder a script is
running in?

I was looking at the globals in $_SERVER but all the variables also
listed the actual file the script was running in.
How about dirname($_SERVER['SCRIPT_FILENAME'])?

This is in no way close to trusted information about actual directory script
running in.
[...]
Solution is
$cwd = realpath('.');
It working fine under Windows. Hope it is true for other systems.
Still not.
>>I was looking at the globals in $_SERVER but all the variables also
listed the actual file the script was running in.
As stated, SCRIPT_FILENAME uses the script the user is *currently*
running even if the instruction was written in a second file, somewhere
else, that is just included.
Unfortunately, realpath('.') has the same behavior.

dirname(__FILE__) is a solution, it returns the directory of the scripts
that implements it, not the one that was called, since __FILE__
specify the filename of the script using it.

if C:\www\test.php includes C:\www\test\test2.php, and you add those
commands to test2.php:
echo $_SERVER['SCRIPT_FILENAME'] ="C:/www"
echo realpath('.') ="C:\www"
echo dirname(__FILE__) ="C:\www\test"

It often bugged me in the past :)

--
Guillaume
Feb 22 '08 #5

P: n/a
On Fri, 22 Feb 2008 02:56:58 +0100, Scott <fu*********@att.netwrote:
What is the most reliable method to determine the folder a script is
running in?

I was looking at the globals in $_SERVER but all the variables also
listed the actual file the script was running in.
Current working directory: getcwd()
Originating file: $_SERVER array.
--
Rik Wasmus
Feb 22 '08 #6

P: n/a
Greetings, Guillaume.
In reply to Your message dated Friday, February 22, 2008, 11:22:59,
>>>What is the most reliable method to determine the folder a script is
running in?

I was looking at the globals in $_SERVER but all the variables also
listed the actual file the script was running in.
How about dirname($_SERVER['SCRIPT_FILENAME'])?

This is in no way close to trusted information about actual directory script
running in.
[...]
>Solution is
$cwd = realpath('.');
It working fine under Windows. Hope it is true for other systems.
Still not.
>>I was looking at the globals in $_SERVER but all the variables also
>>listed the actual file the script was running in.
As stated, SCRIPT_FILENAME uses the script the user is *currently*
running even if the instruction was written in a second file, somewhere
else, that is just included.
Unfortunately, realpath('.') has the same behavior.
dirname(__FILE__) is a solution, it returns the directory of the scripts
that implements it, not the one that was called, since __FILE__
specify the filename of the script using it.
Question was
>>>What is the most reliable method to determine the folder a script is
running in?
not "directory the script located in".

Go realize difference. Or read my previous answer carefully.
if C:\www\test.php includes C:\www\test\test2.php, and you add those
commands to test2.php:
echo $_SERVER['SCRIPT_FILENAME'] ="C:/www"
echo realpath('.') ="C:\www"
echo dirname(__FILE__) ="C:\www\test"
It often bugged me in the past :)

--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Feb 26 '08 #7

P: n/a
"the folder a script is running in"
There is a difference, I know. But it's not in the sentence, it's in the
way you understand it. You can *also* understand the folder the
*current* script you're writing is running in, which means the location
of the currently edited script.

Your solution was good, so is mine, depending on what's really
requested. And I thought I'd better complete it.

Btw my "still not" mainly refered to your "trusted information". Cause
if one is used to use realpath('.') to get the "working" folder, he/she
may trust it the day he/she needs the real script folder. Which would be
wrong. Your solution is working, but "generally" and according to what
you want to do, it may not be trusted either.

That's also why I said "dirname(__FILE__) is *a* solution".

Regards,

--
Guillaume
Feb 26 '08 #8

P: n/a
Greetings, Guillaume.
In reply to Your message dated Tuesday, February 26, 2008, 12:07:13,
Btw my "still not" mainly refered to your "trusted information". Cause
if one is used to use realpath('.') to get the "working" folder, he/she
may trust it the day he/she needs the real script folder.
Which would be wrong. Your solution is working, but "generally" and
according to what you want to do, it may not be trusted either.
That's also why I said "dirname(__FILE__) is *a* solution".
realpath('.'); does exactly waht it does - returning real path to the current
working directory.
And other ppl already posted more correct solution with getcwd(); (CWD -
Current Working Directory). (I've missed it as it was in directory functions,
not in filesystem)
Equally, dirname(__FILE__); does what it does - returning a path to the
directory, where script located.
Now, please explain "the real script folder". What it means?
--
Sincerely Yours, AnrDaemon <an*******@freemail.ru>

Feb 26 '08 #9

P: n/a
AnrDaemon a écrit :
realpath('.'); does exactly waht it does - returning real path to the current
working directory.
Well, I prefer, as you also stated, the getcwd function, cause it really
"says" what it does (aka CWD, which acronym I knew anyway ^^).

"working directory" is just the clearest thing to explain this matter.
"the folder a script is running in" wasn't, nor is "realpath", neither
was my "real script folder" (referring to the folder the script is
located in - since you asked ^^).

Anyway, no need to spend ages on words and sentences, that won't change
anything for php. Only users possibly referring to this topic should
know whether to search for a working directory or a "location"
directory. Thus, I wanted to deal with this issue, that may occur anytime.

Regards,
--
Guillaume
Feb 26 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.