Php delete directory and all files and subdirectories

I need a script which can remove a whole directory with all their subfolders, files and etc. I tried with this function which I found in internet before few months ago but it not work completely.

function deleteFile($dir) {
    if(substr($dir, strlen($dir)-1, 1) != '/') { 
        $dir .= '/'; 
    }
    if($handle = opendir($dir)) { 
        while($obj = readdir($handle)) { 
            if($obj != '.' && $obj != '..') { 
                if(is_dir($dir.$obj)) { 
                    if(!deleteFile($dir.$obj)) {
                        echo $dir.$obj."
"; return false; } } elseif(is_file($dir.$obj)) { if(!unlink($dir.$obj)) { echo $dir.$obj."
"; return false; } } } } closedir($handle); if(!@rmdir($dir)) { echo $dir.'
'; return false; } return true; } return true; }

For the test I use a unpacked archive of prestashop and I try to delete the folder where archive is unpacked but it doesn't work.

/home/***/public_html/prestashop/img/p/3/
/home/***/public_html/prestashop/img/p/3
/home/***/public_html/prestashop/img/p
/home/***/public_html/prestashop/img

These are the problem folders. At the first time I think - "May is a problem with the chmod of the files" but when I test with all files chmod permission 755 (after that with 777) - the result was the same.

Hi! Today let's see how to delete all files and sub-folders from a folder using php. If your website allows user uploaded contents then I bet you might want to do regular clean ups to free up more space in web server. Like deleting all files of a user without activity for long time, deleting files older than 3 months etc. Whatever the reason, doing such clean-up manually is a tedious task. You can accomplish it with the help of an automated php script. And I'm going to show you here exactly how to delete directory and all files and sub directories from it with php.

Php delete directory and all files and subdirectories

Delete All Files from Folder in PHP:

As for deleting all files from a folder the process is pretty straight forward. You only need two native php functions glob() and unlink() to achieve this.

  • Function glob() returns an array of filenames/directories matching the given pattern.
  • Function unlink() deletes a given file.

Our process goes like this - you have to loop through the files one by one from a given folder, check if it is a file and delete file with the help of unlink() function. Here is the simple php function I have written to delete all files from a directory.

PHP Function to Delete All Files from Folder:

Usage:

As you can see in function deleteFiles() we have used the pattern '/*' to instruct glob() to get all the files names from the folder.

Next we loop through the files one by one and check if the item is a file and not a sub folder itself using is_file() function.

Once we confirm it as a file we delete it with unlink() method. The process will be repeated until no file is left in the specific folder.

Delete Only Particular File Type from Folder:

Say you don't want to remove all files, just the pdf files alone from a folder. Then here's the script for the task.

Delete Files Older than X Days from Folder:

This is another useful variation of the script. Suppose you want to delete older files say of at least 90 days old. You have to simply check if the last modified date and time of the file is greater than 90 days and delete if it is.

 (60 * 60 * 24 * 90))
                unlink($file);
        }
    }
}
?>

To check if files are older than 90 days, we have used filemtime() to get last modified timestamp of a given file, took its difference from current timestamp and checked it against 90 days count.

Delete All Files and Sub Folders from a Folder in PHP:

So far we have seen about deleting just files present in a directory. What if you have sub directories with files inside the folder. In such case the above script won't work. You'll need to create recursive function to delete all files, sub-directories and parent directory altogether.

Simply deleting a folder with rmdir() won't work. It will throw exception if you attempt to remove folder with files in it. So first you have to delete files one by one from each sub folder plus the rest of the files and then delete folders by removing parent folder.

Here is the php recursive function to delete files and sub folders from the folder.

Function Usage:

It will take care of deleting all files, sub folders from a folder automatically.

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. I hope you find this tutorial useful. Meet you in another interesting one :)

How to delete all files and folders in a directory PHP?

// function to delete all files and subfolders from folder. function deleteAll($dir, $remove = false) { ... .
if (is_array($structure)) { ... .
deleteAll($file,true); ... .
} ... .
rmdir($dir); ... .
// folder path that contains files and subfolders. ... .
// call the function. ... .
echo "All files and subfolders deleted successfully.";.

How do I empty a directory in PHP?

The rmdir() function in PHP is an inbuilt function which is used to remove an empty directory. It is mandatory for the directory to be empty, and it must have the relevant permissions which are required to delete the directory.

How do I remove folders and contents recursively?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

How do you create and delete directories in PHP?

PHP creating & deleting directories It takes two parameters; path to desired directory and the permission. Mkdir(*/temp/sample*, 0777); Deleting directories: PHP's rmdir() can be used to delete directories.