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. Show Table of Contents
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 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.
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:. |