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 list foreach ( $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 folder array_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 list foreach ( $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);
- 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 folder foreach ( $dir as $file ) { $file ->isDir() ? rmdir ( $file ) : unlink( $file ); } ?> |