Summary: in this tutorial, you will learn how to handle directories using various PHP functions.
PHP directory operations
PHP provides a set of handy functions that allow you to work with directories in the same way as files. You manipulate a directory through a directory handle that is a special variable pointing to a directory. To get the directory handle, you pass the directory path to the opendir() function as follows:
1 2 | <?php $dh = opendir('./backup'); |
If there was an error opening the directory, the opendir() function returns false. Once finish processing a directory, you use the closedir() function to close the directory handle:
1 2 | <?php closedir($dh); |
Each directory may contain a list of files or sub-directories, as well as the entry . that represent the current directory and the entry .. that represents the parent directory.
To get the next entry (a file or a directory) in a directory, you pass the directory handle to the readdir() function. The following example shows you how to list all files in the backup directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php $dh = opendir('./backup'); if($dh){ echo '<ul>'; while($e = readdir($dh)){ if($e != '.' && $e != '..'){ echo '<li>' . $e . '</li>'; } } echo '</ul>'; } closedir($dh); |
PHP current directory
The default current directory is the directory of the running script file. The current directory is important because it is used a the based directory for relative file path.
You can get the current directory by using the getcwd() function.
1 2 3 | <?php echo getcwd(); |
To change the current directory to a new directory, you use the chdir() function:
1 2 3 4 | <?php chdir('./backup'); echo getcwd(); |
After calling the chdir() function, the current directory changes to the new one. We can verify it by calling the getcwd() function.
PHP create directories
To create a directory, you use the mkdir() function. You just need to pass the path of the directory that you want to create to the mkdir() function. The mkdir() function returns true if it created the directory successfully, otherwise it returns false.
1 2 3 4 5 6 7 8 9 10 | <?php $dir = './backup/2013'; $r = mkdir($dir); if($r){ echo sprintf("%s directory created successfully.",$dir); }else{ echo sprintf("An error occurred during creating directory %s",$dir); } |
Notice that the parent directory must exist.
By default the mkdir() function sets 0777 for the new directory. If you want to set a different permission, you can pass it to the mkdir() function or use the chmod() function.
1 2 3 4 | <?php $dir = './backup/2014'; mkdir($dir,0644); |
For more information on setting permissions on the file or directory, check it out the PHP file permissions tutorial.
PHP delete directories
To delete a directory, you use the rmdir() function. You must have sufficient permissions to remove the directory. In addition, the directory must be empty, which means it does not contain any file or sub-directory.
The following example delete the directory /2014 under the /backup directory:
1 2 | <?php rmdir('./backup/2014'); |
PHP get directory from file path
To get directory path from a file path, you use the dirname() function as follows:
1 2 | <?php echo dirname('./backup/test.txt'); // ./backup |
PHP test a file or directory
PHP treats both a directory and a file is a file. However a directory is a special kind of file. To test a file is a regular file or directory you use a pair of handy functions:
- is_dir() – returns true if the file path refers to a directory.
- is_file() – returns true if the file path refers to a file.
Let’s take a look at the following example:
1 2 3 4 5 6 7 8 9 10 | <?php $path = './backup'; if(is_dir($path)){ echo sprintf('%s is a directory',$path); }else if(is_file($path)){ echo sprintf('%s is a file',$path); }else{ echo sprintf('%s is neither file nor directory',$path); } |
PHP listing files in a directory
The following example illustrates how to list all all file in a directory and its sub-directories recursively. The flow of the script is as follows:
- Read the directory
- If the entry is a directory, add it in a subdirectory array ($dirs). If it is a regular file, add it to the files array (files).
- Display the directory and its files.
- For each subdirectory in the $dirs array, call the function to traverse it recursively.
The following script demonstrates how to list all files and subdirectories recursively:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?php $path = './backup'; traverse_dir($path); function display_files($dir,$files){ echo sprintf("<h2>%s</h2>",$dir); if(count($files) > 0){ echo '<ul>'; foreach ($files as $file) { echo sprintf("<li>%s</li>",$file); } echo '</ul>'; } } function traverse_dir($path){ $dh = opendir($path); $files = array(); $dirs = array(); while($e = readdir($dh)){ if($e != '.' && $e != '..'){ // check if it is a directory $f = $path . '/' . $e; if(is_dir($f)){ $dirs[] = $f; }else if(is_file($f)){ $files[] = $f; } } } closedir($dh); // display current directory and its files display_files($path,$files); // recursively traverse subdirecties foreach ($dirs as $dir) { traverse_dir($dir); } } |
In this tutorial, you have learned how to handle directory using various PHP functions. We also shown you how to traverse a directory recursively to list all files and subdirectories inside it.