Section 13.13. Working with Directories


13.13. Working with Directories

Now that you have mastered working with individual files, it is time to take a look at the larger file systemspecifically, how PHP handles directories . Let's start with something simplelisting the contents of a directory. There are three functions we need to perform this task: opendir( ), readdir( ), and closedir( ). The first of the three takes one parameter, which is the directory you wish to access. If it opens the directory successfully, it returns a handle to the directory, which you should store away somewhere for later use.

The readdir( ) function takes one parameter, which is the handle that opendir( ) returned. Each time you call readdir( ) on a directory handle, it returns the filename of the next file in the directory in the order in which it is stored by the file system. Once it reaches the end of the directory, it will return false. Here is a complete example of how to list the contents of a directory:

     $handle = opendir('/path/to/directory')     if ($handle) {             while (false !=  = ($file = readdir($handle))) {                     print "$file<br />\n";             }             closedir($handle);     } 

At first glance, the while statement might look complicated!= = is the PHP operator for "not equal and not the same type as." The reason we do it this way as opposed to just while ($file = readdir($handle)) is because it is sometimes possible for the name of a directory entry to evaluate to false, which would end our loop prematurely. In that example, closedir( ) takes our directory handle as its sole parameter, and it just cleans up after opendir( ).

13.13.1. Creating Directories

Making a new directory in PHP is done using the mkdir( ) function, which takes a directory name as its first parameter, a permission mode as its second, and true or false as its third, depending on whether you also want to create parent directories (defaults to false). The function returns TRue if the directory was created successfully or false otherwise. For example:

     mkdir("/path/to/my/directory", 0777);     // if /path/to/my exists, this should return true if PHP has the right permissions     mkdir("/path/to/my/directory", 0777, true);     // will create /path, /path/to, and /path/to/my if needed and allowed 

13.13.2. Deleting Directories

PHP has the function rmdir( ) that takes a directory name as its only parameter and will delete the specified directory. However, there is a minor catchthe directory must be empty; otherwise, the call will fail. There is no functionality in PHP to allow you to delete non-empty directories, which means you need to resort to more cunning methodsmany people use complex scripts to go through each directory, deleting files as they go. When it is empty, they use rmdir( ).

I would not recommend thata far easier method is simply to execute the local directory-deleting program, e.g., deltree on Windows, or rm -rf on Unix. However, blindly deleting whole directories using scripts is not recommendedif you are sure you want a directory and all its subdirectories gone, check over it one last time and then delete it by hand.

13.13.3. Reading and Changing the Working Directory

When working from the command line, it is a common requirement to be able to change the current working directory the directory that your PHP script is operating in. To find the current working directory, use getcwd( ). You can then change the working directory using chdir( ), like this:

     $original_dir = getcwd( );     // something like /home/paul     chdir("/etc");     // now we're in /etc     $passwd = fopen("passwd", "r");     // open the /etc/passwd file     fclose($passwd);     chdir($original_dir); 

Both getcwd( ) and chdir( ) return true on success or false on failure.

13.13.4. One Last Directory Function

The scandir( ) function is a neat function that takes a minimum of one parameter with an optional second. Parameter one is the path of a directory you want to work withscandir( ) returns an array of all files and directories in the directory you specify here. Parameter two, if included and set to 1, will sort the array returned reverse-alphabeticallyif it is not set, the array is returned sorted alphabetically.

This next script prints out a list of all the files and directories in the current directory, with reverse sorting:

     $files = scandir(".", 1);     var_dump($files); 

Using scandir( ) is a quick alternative to calling readdir( ) repeatedly, and is particularly helpful when you use the second parameter.



PHP in a Nutshell
Ubuntu Unleashed
ISBN: 596100671
EAN: 2147483647
Year: 2003
Pages: 249

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net