When writing PHP scripts that need to deal with the file-system to change the current directory, creating new directory, change the root directory, gets the current working directory, list files and directories inside the specified path and many more. For this, PHP provides a number of built-in directory functions that can be used to perform such tasks.
Also, read:
- Manipulating PHP arrays: push, pop, shift, unshift
- 15+ regular expressions for PHP developers
- Convert Associative Array into XML in PHP
- Convert XML into Associative Array in PHP
- Converting an Array to JSON in PHP
- Converting JSON to Array or Object in PHP
Let’s go over the 10 most important built-in PHP directory functions.
Creating Directory
A new directory can be created in PHP using the mkdir() function. This function takes a path to the directory to be created. A second, optional argument allows the specification of permissions on the directory.
mkdir("/home/php_files", 0777);
The above example will create directory similar to:
/home/php_files
Gets the current working directory
You can get the current working directory in PHP by using getcwd() function.
echo getcwd();
The above example will output:
/home/php_files
Change directory
The current working directory can be changed using the chdir() function. It takes only one argument the path of the new directory.
chdir('include');
The above example will output:
/home/php_files/include
Change the root directory
The PHP function chroot() changes the root directory of the current process to directory, and changes the current working directory to “/”.
chroot("/home/php_filesinclude/include"); echo getcwd();
The above example will output:
/
Read directory content
The function opendir() opens up directory handle and readdir() will read directory contents.
$dir = "/home/php_files/images/"; // Open up a directory, and read its contents if (is_dir($dir)){ if ($dh = opendir($dir)){ while (($file = readdir($dh)) !== false){ echo "File:" . $file . " "; } closedir($dh); } }
The above example will output:
File: header.png File: home.png File: save.png
Rewind directory handle
The directory function rewinddir() resets the directory stream indicated by dir_handle to the beginning of the directory.
$dir = opendir("/home/php_files/images/"); if ($dh = opendir($dir)){ while (($file = readdir($dh)) !== false){ echo "filename:" . $file . " "; } rewinddir(); while (($file = readdir($dh)) !== false){ echo "filename:" . $file . " "; } }
List files and directories
By using scandir() function, you can list files and directories inside the specified path.
$dir = opendir("/home/php_files/images/"); $files = scandir($dir); print_r($files);
Get an instance of the Directory
Using dir() function, you can get an instance of the Directory class.
$path = dir(getcwd()); print_r($path);
Open directory handle
You can open directory handle using opendir() function to read directory files.
$dir_path = dir(getcwd()); if (is_dir($dir_path)){ if ($dh = opendir($dir_path)){ while (($file = readdir($dh)) !== false){ echo "File:" . $file . " "; } closedir($dh); } }
Close directory handle
You can close directory handle using closedir() function which opened by opendir() function.
$dir_path = dir("/home/php_files/"); if (is_dir($dir_path)) { if ($dh = opendir($dir_path)) { $directory = readdir($dh); closedir($dh); } }
You may also like:
- Working with php.ini file Configuration
- Control Statements in PHP
- Convert Associative Array into XML in PHP
- Convert XML into Associative Array in PHP
- Using Prepared Statement with PHP & MySQL
- How to Upload File in PHP
- Converting an Array to JSON in PHP
- Converting JSON to Array or Object in PHP
- Remove Repeated Words From String in PHP
- Converting a PHP Array to a Query String
- 15+ regular expressions for PHP developers
- 10 little known but useful PHP functions
- PHP Script to Download Large Files Reliably