Determine folder a script is running in.

This is a discussion on Determine folder a script is running in. within the PHP Language forums, part of the PHP Programming Forums category; What is the most reliable method to determine the folder a script is running in? I was looking at the ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-22-2008
Scott
 
Posts: n/a
Default Determine folder a script is running in.

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
Reply With Quote
  #2 (permalink)  
Old 02-22-2008
Jerry Stuckle
 
Posts: n/a
Default Re: Determine folder a script is running in.

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.
jstucklex@attglobal.net
==================

Reply With Quote
  #3 (permalink)  
Old 02-22-2008
Scott
 
Posts: n/a
Default Re: Determine folder a script is running in.

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.
Reply With Quote
  #4 (permalink)  
Old 02-22-2008
AnrDaemon
 
Posts: n/a
Default Re: Determine folder a script is running in.

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 <anrdaemon@freemail.ru>

Reply With Quote
  #5 (permalink)  
Old 02-22-2008
Guillaume
 
Posts: n/a
Default Re: Determine folder a script is running in.

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
Reply With Quote
  #6 (permalink)  
Old 02-22-2008
Rik Wasmus
 
Posts: n/a
Default Re: Determine folder a script is running in.

On Fri, 22 Feb 2008 02:56:58 +0100, Scott <futureshock@att.net> 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.


Current working directory: getcwd()
Originating file: $_SERVER array.
--
Rik Wasmus
Reply With Quote
  #7 (permalink)  
Old 02-26-2008
AnrDaemon
 
Posts: n/a
Default Re: Determine folder a script is running in.

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 <anrdaemon@freemail.ru>

Reply With Quote
  #8 (permalink)  
Old 02-26-2008
Guillaume
 
Posts: n/a
Default Re: Determine folder a script is running in.

"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
Reply With Quote
  #9 (permalink)  
Old 02-26-2008
AnrDaemon
 
Posts: n/a
Default Re: Determine folder a script is running in.

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 <anrdaemon@freemail.ru>

Reply With Quote
  #10 (permalink)  
Old 02-26-2008
Guillaume
 
Posts: n/a
Default Re: Determine folder a script is running in.

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
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 08:34 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0