- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP rmdir() | Remove Directory
The PHP rmdir() function is used when we need to delete or remove a directory. For example:
<?php $directory = "Documents"; $ch = rmdir($directory); if($ch) echo "<p>The directory deleted successfully</p>"; else echo "<p>Unable to delete the directory</p>"; ?>
The output of the above PHP example using the rmdir() function is shown in the following snapshot:
Note: The directory will be removed only when the directory is completely empty.
The above output is produced because, inside my current directory, there is a folder or directory named "Documents" that is, of course, empty.
PHP rmdir() Syntax
The syntax of the rmdir() function in PHP is:
rmdir(directory, context)
The first parameter (directory) is required, whereas the last parameter (context) is optional and is used to specify the context resource while removing an empty directory.
PHP rmdir() function example
Consider the following PHP code as an example demonstrating the rmdir() function:
<?php
$dir_path = "xyz";
// Before attempting to remove a directory, ensure that it exists.
if (is_dir($dir_path)) {
if (rmdir($dir_path)) {
echo "The directory was successfully removed.";
} else {
echo "Unable to remove directory";
}
} else {
echo "Directory does not exist";
}
?>
The step-by-step explanation of the above PHP example is as follows:
- The string value "xyz" is assigned to the variable $dir path. This represents the directory path that we want to remove.
- The is_dir() function determines whether a directory exists. The function call returns true if the directory exists; otherwise, it returns false.
- If the directory exists, the code goes into the "if" block and uses the rmdir() function to remove it. If the removal is successful, the function call returns true and displays the success message "The directory was successfully removed." If the removal fails, the function call returns false and displays the error message "Unable to remove directory."
- If the directory does not exist, the code enters the else block and displays the message "Directory does not exist."
Advantages of the rmdir() function in PHP
- Since PHP already includes this function, using it is simple and doesn't call for any additional libraries or modules.
- It is an easy and clear way to delete a directory, and empty directories are automatically deleted as well.
- It gives back a boolean value to show whether the removal was successful or not, enabling conditional logic and error handling in your code.
Disadvantages of the rmdir() function in PHP
- Only empty directories can be deleted using the rmdir() function. The function call will fail and the directory won't be deleted if it contains any files or subdirectories.
- Before using rmdir(), you must manually remove any files or subdirectories from the directory, which can be time-consuming and error-prone.
- Debugging may be more challenging because the function doesn't explain why the removal failed.
« Previous Tutorial Next Tutorial »