PHP Directory

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:

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:

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:

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.

To change the current directory to a new directory, you use the chdir() function:

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.

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.

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:

PHP get directory from file path

To get directory path from a file path, you use the dirname() function as follows:

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:

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:

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.

  • Was this tutorial helpful ?
  • YesNo