Cara menggunakan php unlink directory

In this post, I will show you how you can delete all files and folders from a folder using PHP. This is pretty straight-forward, as we simply loop through the files and delete them.

Table of Contents

  • Deleting Selected files through checkboxes
  • Here is the complete code.
  • Marking for deletion
  • Listing the files after deleting
  • How can I delete all files in a directory in PHP?
  • How can you delete file from PHP?
  • Which function is used in PHP to delete a file?
  • HOW include all files in a directory in PHP?

First of all, we would like to show you how to delete only files from a folder.

<?php

//The name of the folder.
$folder = 'files';

//Get a list of all of the file names in the folder.
$files = glob($folder . '/*');

//Loop through the file list.
foreach($files as $file){
//Make sure that this is a file and not a directory.
if(is_file($file)){
//Use the unlink function to delete the file.
unlink($file);
}
}

Earlier we have seen about deleting just files which are present in a directory. Now, What if you have sub directories with files inside the folder. In that case the above script won’t able to work. You’ll need to create recursive function to delete all files, sub-directories and parent directory altogether.

Simply deleting a folder with using php’s function rmdir() won’t work. It will throw some exceptions if you attempt to remove folder directly with files in it. So first of all you have to delete files one by one from each sub folder and then delete folders by removing parent folder.

So, here is the recursive function to delete files and sub folders from the folder.

<?php
// delete all files and sub-folders from a folder
function deleteAll($dir) {
foreach(glob($dir . '/*') as $file) {
if(is_dir($file))
deleteAll($file);
else
unlink($file);
}
rmdir($dir);
}
?>

This is the function which will help you out to delete folder and sub folders and also files. You can use this function by simply calling:

<?php deleteAll('folder_name'); ?>

The function above basically checks to see if the $file variable represents a path to a file. If it does represent a path to a file, it deletes the file using the function unlink. However, if $file represents a directory, then it gets a list of all files in said directory before deleting each one. Finally, it removes the sub-directory itself by using PHP’s rmdir function.

You can use the above php script to delete files and sub-directories in a given folder programmatically. The script is relatively fast but it depends on the amount of files and folders to be deleted.

We have seen how a file can be deleted by using unlink function in PHP. The same function can be used along with directory handler to list and delete all the files present inside. We have discussed how to display all the files present inside a directory. Now let us try to develop a function and to this function we will post directory name as parameter and the function will use unlink command to remove files by looping through all the files of the directory.

Here is the code to this.


function EmptyDir($dir) {
$handle=opendir($dir);

while (($file = readdir($handle))!==false) {
echo "$file <br>";
@unlink($dir.'/'.$file);
}
closedir($handle);
}
EmptyDir('images');

Here images is the directory name we want to empty

Deleting Selected files through checkboxes

As we know how to display all files of a directory , we will use same concept to fist display the list of files. While displaying the file names we will add one checkbox before it for selection of file by user for deletion. For this we have used one array of checkbox.
echo "<form method=post name='f1' action=''>";
while (list ($key, $val) = each ($ar)) {
if(strlen($val)>3){
echo "<input type=checkbox name=box[] value='$val'>$val<br>";
}
}
echo "<input type=submit value='Delete'></form>";
On submit of the form we can loop through the array of checkbox to identify files which are selected by user.
while (list ($key,$val) = @each ($box)) {
$path=$dir	."/".$val;
if(unlink($path)) echo "Deleted file ";
echo "$val,";
}

Then we will select the path and delete the selected files by using unlink command.

Here is the complete code.

<?Php
$dir='test_dir'; // directory name 
$ar=scandir($dir); 
$box=$_POST['box'];  // Receive the file list from form

// Looping through the list of selected files ///
while (list ($key,$val) = @each ($box)) {
$path=$dir	."/".$val;
if(unlink($path)) echo "Deleted file ";
echo "$val,";
}
echo "<hr>";

/// displaying the file names with checkbox and form ////
echo "<form method=post name='f1' action=''>";
while (list ($key, $val) = each ($ar)) {
if(strlen($val)>3){
echo "<input type=checkbox name=box[] value='$val'>$val<br>";
}
}
echo "<input type=submit value='Delete'></form>";
?>

Marking for deletion

We can modify the above script and instead of deleting the file we can display them with strikeout text showing marked for deletion. On further confirmation from the user same can be deleted.

Listing the files after deleting

In the above code we are reading or scanning all files of the directory by using scandir function.
$ar=scandir($dir);
We are only once using this and after deleting we can again check the directory by using scandir function. So you can add this line after deleting the files.
<?Php
$dir='test_dir'; // directory name 
$ar=scandir($dir); 
$box=$_POST['box'];  // Receive the file list from form

// Looping through the list of selected files ///
while (list ($key,$val) = @each ($box)) {
$path=$dir	."/".$val;
if(unlink($path)) echo "Deleted file ";
echo "$val,";
}
echo "<hr>";
$ar=scandir($dir);// Once again directory content is scanned to exclude deleted files. 
/// displaying the file names with checkbox and form ////
echo "<form method=post name='f1' action=''>";
while (list ($key, $val) = each ($ar)) {
if(strlen($val)>3){
echo "<input type=checkbox name=box[] value='$val'>$val<br>";
}
}
echo "<input type=submit value='Delete'></form>";
?>



Subscribe

* indicates required

Email Address *

First Name

Last Name

Subscribe to plus2net


    plus2net.com
    Click here for More on PHP File functions
    dhiraj chavan

    10-10-2014

    nice...thanx to plus2net
    amir

    01-03-2017

    thanks. the files are deleted, but it has some warning. it can't read 'box'. why ?
    smo1234

    03-03-2017

    Not clear what is the box here, do you have any file name or any thing else. Tested the script it is working fine.
    After deleting the same old files are shown again. So the script is modified to take a fresh scan of directory after deleting the files.

    Post your comments , suggestion , error , requirements etc here .

    Detail

    How can I delete all files in a directory in PHP?

    Filter the list using array_filter() or array_merge() methods. Map the list to unlink() method using array_map() method..

    Generate a list of files using glob() method..

    Iterate over the list of files..

    Check whether the name of files is valid..

    Delete the file using unlink() method..

    How can you delete file from PHP?

    To delete a file in PHP, use the unlink function. Let's go through an example to see how it works. The first argument of the unlink function is a filename which you want to delete. The unlink function returns either TRUE or FALSE , depending on whether the delete operation was successful.

    Which function is used in PHP to delete a file?

    PHP | unlink() Function The unlink() function is an inbuilt function in PHP which is used to delete files.

    HOW include all files in a directory in PHP?

    In order to include all PHP files from directory at once need a foreach-loop..

    Example: This example contains four PHP files (file1. ... .

    Create file1.php in folder 'myGeeks':.

    Create file2.php in folder 'myGeeks':.

    Create file3.php in folder 'myGeeks':.

    Create file4.php in folder 'myGeeks':.

    Create main.php outside folder:.