Deleting all files from a folder using PHP

08 Jan, 2019
In PHP, files from a folder can be deleted using various approaches and inbuilt methods such as unlink, DirectoryIterator and DirectoryRecursiveIterator.Some of these approaches are explained below:
Approach 1:
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.
Example:
<?php// PHP program to delete all// file from a folder   // Folder path to be flushed$folder_path = "myGeeks";   // List of name of files inside// specified folder$files = glob($folder_path.'/*');    // Deleting all the files in the listforeach($files as $file) {       if(is_file($file))             // Delete the given file        unlink($file); }?>
Output:Before Running the code:After Running the code:Note: Hidden files can be included in the file removal operation by addition of the below code:
$hidden_files = glob($folder_path.'/{, .}*', GLOB_BRACE);
Approach 2:
Generate a list of files using glob() method.
Filter the list using array_filter() or array_merge() methods.
Map the list to unlink() method using array_map() method.
Example:
<?php// PHP program to delete all files from a folder  // Deleting all the files inside the given folderarray_map('unlink', array_filter(            (array) array_merge(glob("myGeeks/*"))));  ?>
Approach 3:
Generate list of files using DirectoryIterator.
Iterate over the list of files.
Validate the file while checking if the file directory has a dot or not.
Using the getPathName method reference, delete the file using unlink() method.
Example:
<?php// PHP program to delete all files// from a folder  // Folder path to be flushed$folder_path = 'myGeeks/';  // Assigning files inside the directory$dir = new DirectoryIterator(dirname($folder_path));  // Deleting all the files in the listforeach ($dir as $fileinfo) {          if (!$fileinfo->isDot()) {          // Delete the given file        unlink($fileinfo->getPathname());    }}?>
Approach 4:
Generate list of all directory hierarchies inside the given folder directory using RecursiveDirectoryIterator$dir = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
$dir = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST);
$dir = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
$dir = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::CHILD_FIRST);
FilesystemIterator::SKIP_DOTS is used to ignore dots while generating list of files.
RecursiveIteratorIterator::CHILD_FIRST is used for choosing files present at root directory.
Iterate over the file list and remove folders and files according to the specification.
Example:
<?php// PHP program to delete all FilesystemIterator// from a folder   // Folder path to be flushed$dir = "myGeeks/";   // Assigning files inside the directory$dir = new RecursiveDirectoryIterator(    $dir, FilesystemIterator::SKIP_DOTS);   // Reducing file search to given root// directory only$dir = new RecursiveIteratorIterator(    $dir,RecursiveIteratorIterator::CHILD_FIRST);   // Removing directories and files inside// the specified folderforeach ( $dir as $file ) {     $file->isDir() ?  rmdir($file) : unlink($file);}?>
PHP-file-handling
Picked
PHP
PHP Programs
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to execute PHP code using command line ?
How to Insert Form Data into Database using PHP ?
PHP in_array() Function
How to delete an array element based on key in PHP?
How to pop an alert message box using PHP ?
How to execute PHP code using command line ?
How to Insert Form Data into Database using PHP ?
How to delete an array element based on key in PHP?
How to pop an alert message box using PHP ?
How to convert array to string in PHP ?