I would like to delete all files matching a particular extension in a specified directory and all subtree. I suppose I should be using using unlink but some help would be highly appreciated... Thank you!
Asked
Active
Viewed 1,393 times
0
-
yes `unlink()` will help you for deleting files and `Delete` will help, deleting directories :) – prava Jun 02 '14 at 13:09
-
this might help you - `http://stackoverflow.com/questions/1334398/how-to-delete-a-folder-with-contents-using-php` – prava Jun 02 '14 at 13:11
2 Answers
0
you need a combination of this
And the unlink
/ delete
You should be able to edit the example instead of echoing the file, to delete it
0
To delete specific extension files from sub directories, you can use the following function. Example:
<?php
function delete_recursively_($path,$match){
static $deleted = 0,
$dsize = 0;
$dirs = glob($path."*");
$files = glob($path.$match);
foreach($files as $file){
if(is_file($file)){
$deleted_size += filesize($file);
unlink($file);
$deleted++;
}
}
foreach($dirs as $dir){
if(is_dir($dir)){
$dir = basename($dir) . "/";
delete_recursively_($path.$dir,$match);
}
}
return "$deleted files deleted with a total size of $deleted_size bytes";
}
?>
e.g. To remove all text files you can use it as follows:
<?php echo delete_recursively_('/home/username/directory/', '.txt'); ?>

v2solutions.com
- 1,439
- 9
- 8