Page 357 - Beginning PHP 5.3
P. 357

Chapter 11: Working with Files and Directories
                            chdir()  returns  true  if PHP managed to change to the specified directory, or  false  if there was an
                         error (such as the directory not being found).

                           The current directory is the directory where PHP first looks for files. If you specify a path that isn ’ t an
                         absolute or relative path, PHP looks for the file inside the current directory. So the following code
                             chdir( “/home/matt/myfolder” );
                             $handle = fopen( “myfile.txt” );
                         opens the same  myfile.txt  file as:

                             $handle = fopen( “/home/matt/myfolder/myfile.txt” );

                           The current directory is also used as the base directory for relative file paths. For example:
                             chdir( “/home/joe/images” );
                             $handle = fopen( “../myfile.txt” );  // Looks for myfile.txt in /home/joe

                           Usually the current directory defaults to the directory containing the running script. You can retrieve the
                         current directory by calling   getcwd() :
                             chdir( “/home/matt/newfolder” );


                             echo getcwd();  // Displays “/home/matt/newfolder”
                           Creating Directories
                          To create a new directory, call the  mkdir()  function, passing in the path of the directory you want to create:
                             mkdir( “/home/matt/newfolder” );


                           Note that the parent directory has to exist already (  “ /home/matt ”  in the example just shown) for the
                         function to work.   mkdir()  returns  true  if the directory was created, or  false  if there was a problem.
                           You can also set permissions for the directory at the time you create it by passing the mode as the second
                         argument. This works much like using   chmod()  —  see the  “ Changing Permissions ”  section earlier in the

                          chapter for details. For example, the following code creates a directory with read, write, and execute
                          permissions granted to all users:

                             mkdir( “/home/matt/newfolder”, 0777 );
                             File and directory modes only work on UNIX systems such as Linux and Mac OS; they have no effect
                             when used on Windows machines.

                           Deleting Directories
                          The  rmdir()  function removes a given directory. The directory must be empty, and you need
                         appropriate permissions to remove it. For example:


                             rmdir( “/home/matt/myfolder” );

                           If PHP can ’ t remove the directory  —  for example, because it ’ s not empty  —  rmdir()  returns  false ;
                          otherwise it returns   true .
                                                                                                         319





                                                                                                      9/21/09   9:10:18 AM
          c11.indd   319
          c11.indd   319                                                                              9/21/09   9:10:18 AM
   352   353   354   355   356   357   358   359   360   361   362