3 Replies Latest reply on Aug 1, 2007 7:07 PM by Newsgroup_User

    Chdir function in php?

    Level 7
      I am running php 5.1.6 on my local server (under Windoze), and put the following
      bit of code in a test program:

      $orig = getcwd ();
      chdir ('../../$orig');
      $new = getcwd ();

      This gave the diagnostic:

      Warning: chdir() [function.chdir]: No such file or directory (errno 2) in
      D:\Websites\Corybas\Cydalba\Gen_page.php. getcwd worked as expected.

      I found this function in the online manual at the address below. It does not
      indicate any limitations on this function. What am I doing wrongly?

      http://au3.php.net/manual/en/function.chdir.php

      Clancy
        • 1. Re: Chdir function in php?
          Level 7
          Clancy wrote:
          > I am running php 5.1.6 on my local server (under Windoze), and put the following
          > bit of code in a test program:
          >
          > $orig = getcwd ();
          > chdir ('../../$orig');
          > $new = getcwd ();

          There are two problems with what you're trying to do. First of all,
          you're using getcwd() incorrectly. getcwd() gets the full path of the
          current working directory. If you used echo with getcwd(), you would see
          the problem immediately. What you're attempting to do with chdir() is
          this ../../C:\htdocs\test. Won't work.

          The second problem is that you're attempting to interpolate a variable
          in single quotes. Again, it won't work. Single quotes treat everything
          inside them as a literal string. So, you're attempting to change to a
          directory called $orig. To interpolate a variable in a string, you must
          use double quotes.

          --
          David Powers, Adobe Community Expert
          Author, "The Essential Guide to Dreamweaver CS3" (friends of ED)
          Author, "PHP Solutions" (friends of ED)
          http://foundationphp.com/
          • 2. Re: Chdir function in php?
            Level 7
            .oO(Clancy)

            >I am running php 5.1.6 on my local server (under Windoze), and put the following
            >bit of code in a test program:
            >
            > $orig = getcwd ();
            > chdir ('../../$orig');
            > $new = getcwd ();

            Just a little side note: There shouldn't be blanks between a function
            name and its arguments. That's considered bad coding style.

            >This gave the diagnostic:
            >
            >Warning: chdir() [function.chdir]: No such file or directory (errno 2) in
            >D:\Websites\Corybas\Cydalba\Gen_page.php. getcwd worked as expected.

            Did you check the return value of getcwd()? It returns the full path to
            the current working directory. Given the example path above, your code
            would lead to /Websites/Websites/Corybas/Cydalba, which obviously
            doesn't exist.

            What are you trying to do anyway?

            Micha
            • 3. Re: Chdir function in php?
              Level 7
              Michael Fesser <netizen@gmx.de> wrote:

              Thank you both for your helpful comments. I promise that in future I will try
              to refrain from sending any questions until I have thought about the problem for
              24 hours.

              I misread the diagnostic, and thought that it said that function chdir did not
              exist. I could not understand how this could be the case, so I asked the
              question. If I had not misread the diagnostic I would soon have realised that I
              was doing something silly.

              >What are you trying to do anyway?

              I am preparing to write a set of utilities to process the date for my photo
              album program. These will run in a subdirectory, so I thought I would have to
              change the reference back to the root directory to enable various functions
              written for the program itself to operate correctly. I am not sure if this is
              actually so, but will try it out before I ask any more silly questions.

              Clancy