- 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 exit()
- PHP exit() vs. break
- PHP isset()
- 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 mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- 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 Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- 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 »