Determine folder a script is running in.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott

    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
  • Jerry Stuckle

    #2
    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($_SERVE R['SCRIPT_FILENAM E'])?

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Scott

      #3
      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($_SERVE R['SCRIPT_FILENAM E'])?
      >
      That worked perfectly, thanks Jerry.

      Comment

      • AnrDaemon

        #4
        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($_SERVE R['SCRIPT_FILENAM E'])?
        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($_SERVE R['SCRIPT_FILENAM E']);

        file_put_conten ts('test.$$$', $s);

        ?>

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

        Then, contents of C:\test.$$$ was
        C:\Profiles\<Us ername>
        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@free mail.ru>

        Comment

        • Guillaume

          #5
          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($_SERVE R['SCRIPT_FILENAM E'])?
          >
          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\tes t2.php, and you add those
          commands to test2.php:
          echo $_SERVER['SCRIPT_FILENAM E'] ="C:/www"
          echo realpath('.') ="C:\www"
          echo dirname(__FILE_ _) ="C:\www\tes t"

          It often bugged me in the past :)

          --
          Guillaume

          Comment

          • Rik Wasmus

            #6
            Re: Determine folder a script is running in.

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

            Comment

            • AnrDaemon

              #7
              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($_SERVE R['SCRIPT_FILENAM E'])?
              >>
              >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\tes t2.php, and you add those
              commands to test2.php:
              echo $_SERVER['SCRIPT_FILENAM E'] ="C:/www"
              echo realpath('.') ="C:\www"
              echo dirname(__FILE_ _) ="C:\www\tes t"
              It often bugged me in the past :)



              --
              Sincerely Yours, AnrDaemon <anrdaemon@free mail.ru>

              Comment

              • Guillaume

                #8
                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

                Comment

                • AnrDaemon

                  #9
                  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@free mail.ru>

                  Comment

                  • Guillaume

                    #10
                    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

                    Comment

                    Working...