How to Delete Directory Using PHP

How to delete directory using php - To delete the directory used function rmdir (). This function can only be used to remove empty directories. For the directory containing the files should be deleted all the files in it, then deleted the empty directory.
How to write function rmdir ()
rmdir (name, context)
In order to better understand, the following I give examples of programs to remove the directory.
Program
File Name: file10.php
Description: The program deletes the directory.
Open notepad, then type the following php code.
<?php
//create a directory
$dir = "include";  //directory name
$cek = mkdir ($dir);
if ($cek) {
 echo "Direktori <b>$dir</b> Successfully created";  
 } else {  
 echo "Direktori <b>$dir</b> Failed to create";  
 }
//delete directory
$del = rmdir ($dir);
if ($del) {
 echo "<br>Direktori <b>$dir</b> Successfully deleted";  
 } else {
 echo "<br>Direktori <b>$dir</b> Failed to delete"; 
 }
?>

How to Delete Directory Using PHP

Save the php code with file10.php name in htdocs folder.
Program Explanation
The above program will create a new directory named "include" (see line 4). After that, the directory will be deleted with the function rmdir () on line 10. The rmdir () function can only remove empty directories. If the directory contains contents then, use the recursive function to delete the contents of the directory. See the example in PHP Manual.
To run the program, open the browser then type http://localhost/file10.php in the address bar then enter. Then the results will look like the following.
How to Delete Directory Using PHP
Program view

That's the tutorial on how to delete directory using php that I can share. Hope can help you and improve our ability in learning php programming language. See also tutorial on: How to Display Directory Content Using PHP.

Comments