Last active
July 20, 2024 20:37
-
-
Save gfucci/0c4b84fc25b40337d130ceb666a78621 to your computer and use it in GitHub Desktop.
Delete files recursively by extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// deletes all files with the chosen extensions. The script will run on all folders and subfolders starting from the folder where you ran the script (don't forget to make a backup first) | |
ini_set('memory_limit', -1); | |
$currentPath = getcwd(); | |
$extensions = [ | |
//put here extensions to delete | |
]; | |
$count = 0; | |
function deleteFilesRecursively($currentPath, $extensions, $count) | |
{ | |
global $count; | |
$folder = glob($currentPath . '/*'); | |
foreach ($folder as $key => $folderFile) { | |
if (!is_dir($folderFile)) { | |
$path_parts = pathinfo($folderFile); | |
if (array_key_exists('extension', $path_parts) | |
&& in_array($path_parts['extension'], $extensions) | |
) { | |
$count++; | |
echo "arquivo: $folderFile" . PHP_EOL; | |
unlink($folderFile); | |
} | |
} else { | |
deleteFilesRecursively($folderFile, $extensions, $count); | |
} | |
} | |
} | |
deleteFilesRecursively($path, $extensions, $count); | |
echo "Deleted files: $count" . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment