In PHP there will be many times in the web applications that you create you will need to deal with the server file system at some point. Whether that's manipulating files or folders, they are both very common tasks within PHP. We have already covered how you can create a new directory in PHP, and in this guide, we will explore how you can delete and remove a directory in PHP, safely, all with code examples to show you the way, using the pre-built PHP function, rmdir.
What is the rmdir function?
The rmdir function in PHP stands for remove directory and is PHP's built-in function that can be used by PHP programmers within their codebase. Available since PHP 4, and still supported in the newest versions of PHP 8+, the rmdir function is a great way for PHP to delete a directory, whilst honoring its contents. One great feature of this function is, that when attempting to remove a directory, if that directory contains files, it will fail. This protects against accidental deletions and is typical behavior within Unix-based systems, (when using the command 'rm') where folders cannot be removed if they contain files. The rmdir makes up a collection of common PHP functions that allow you to perform file and folder manipulation. Below is a list of these file system-level functions available to use within PHP.
- copy - copy files and directories in PHP
- rename - renames files and directories in PHP
- mkdir - create directories in PHP
- unlink - deletes a file in PHP
How does the PHP rmdir function work?
The rmdir function in PHP works by accepting a string directory path, which would be the folder you would like to remove. The function will return a boolean either true on success or false if it failed. If you did receive a false boolean, it could mean either the directory is not empty or doesn't exist.
# This will remove directory 'test'
rmdir('test');
Although less common, it is also possible to tell PHP the context stream resource (such as providing authentication credentials), using the stream_context_create function and it should be of type resource. This is an optional field and can be left blank. It's also good practice to check the folder exists in the location you think it should be to avoid removing the wrong thing. This is also a good idea to check you are trying to remove a folder instead of a file. To do that, you can use the 'is_dir' function, which will return true if the provided directory path is a folder and exists. Doing this is not only good practice but also helps keep your PHP code future-proofed and easier for other developers to understand your logic. If later down the line you need more complex logic or processing, checking the directory exist will put you in a good stead.
# This will check the directory exists before removing
$dirName = 'test';
if (is_dir($dirName)) {
rmdir($dirName);
}
There's an important check you must do before running rmdir on a directory. If the directory contains other files or folders, it will return a failure and an E_WARNING level error into the PHP 'error_log'. Therefore you must ensure the directory is completely empty and that PHP has the relevant permissions permitted to perform the action. To use PHP to delete all files in a directory you can use 'scandir'. The scan directory function in PHP allows you to scan (or search for) all content within a given folder. Couple that with the 'unlink' function, which can be used to remove items, you can loop each item in that folder and ensure it is empty before attempting to remove it using the rmdir function. The following code is an example of how to delete files within a directory with PHP before finally removing that folder. As 'scandir' will in Linux environments return single and double dots (current and parent directory), it's important to not attempt to delete these with PHP. That's why within our loop we are checking that the file doesn't match these.
$dir = 'test';
if ($dir) {
foreach (scandir($dir) as $file) {
if ($file !== '.' && $file !== '..') {
unlink($file);
}
}
rmdir($dir);
}
An alternative way to remove the single and double dots from scandir is by using the array_diff function in PHP. By using array_diff, you can filter out single and double dots and in doing so remove the need for the if statement in the example above. This can help to keep your loop cleaner and means PHP has one less if statement to check. Either way comes down to preference.
$dir = 'test';
array_diff(scandir($dir), ['.', '..']);
Conclusion
The rmdir is a great way to delete a folder in PHP and is a safe way to so as you can wrap other logic around the deletion such as is_dir.
- You must delete all files in a directory with PHP first before attempting to remove a directory in PHP
- PHP must have the relevant permissions on the folder you're trying to remove
- Use the is_dir function to check the directory is actually a directory
- Use scandir to loop the folder structure to remove files before running rmdir