beginning_php thread: Re: chdir() (fully explained)
Message #1 by Francisco de Jesus Orozco Ruiz <fjor@g...> on Wed, 11 Jul 2001 09:19:42 -0500 (CDT)|
I failed to see any explanation about current directory, chdir() and
getcwd(). Here is my little contribution.
The current directory is the default directory when any program
wants to make a reference to a file without the full path of the file.
As an example, suppose my program (or script or whatever) has a
current directory of /var/apache/htdocs. If I refer to a file
like friends.dat it will be looked up in that directory, so,
$fp = fopen("friends.dat", "r");
will open /var/apache/htdocs, because the system guesses it will
be in the current directory. If it is not there, there will be an
The current directory, then, is a mechanism to abbreviate filenames
by not having to write the full path of the file.
The function chdir() lets us change the current directory, so
$result = chdir("/data/files");
will make /data/files the new default path to search for files that
haven't the full path in its name. $result will be FALSE if unable
to change, as when the directory doesn't exists.
The function getcwd() returns the current directory:
$curdir = getcwd();
The variable $curdir will contain the default directory; in this
case, will be "/data/files".
To determine if a file has a fullpath or not, the system looks
if the filename starts with the "root" directory: / in UN*X,
\ in Windows. This way, if the current dir is c:\data\images,
saying \windows\media (is a full path!) will be used
directly. But using windows\media will prepend the current directory
to build c:\data\images\windows\media as the filename!
Note: Some of these functions appear in PHP 4 or newer! (even
exist a "posix_getcwd()" for UN*X systems)
For more information and useful tips, I suggest to visit:
and write any of this functions in the search box and
press enter. It'll show the sintax, versions where they are
valid and code chunks using them.
Message #2 by Paul Agin <pagin@k...> on Wed, 11 Jul 2001 10:01:31 -0400|
OK, here's even more details...
In Windows, the current directory is always checked first. PHP4 acts like
Windows in this respect. Also, if the file is not found in the current
directory, PHP4 will search the "include_path" specified in the "php.ini" file.
(In Linux, unless the current directory is in your path, you must use "./" in
front of the file. The Windows method is convenient, but the Linux method is
Using an absolute path: cd /var/www/html/PHPCode
This changes directory to /var/www/html/PHPCode no matter where you start from.
Using a relative path: cd ch15
Moves up one directory from /var/www/html/PHPCode to /var/www/html/PHPCode/ch15.
Using a relative path: cd ../../../cgi-bin
This moves back three directories, then it moves up to the /var/www/cgi-bin
directory, which is parallel to the /var/www/html directory.
As stated before, the current working directory is the directory that your
script is in when you execute it.