Skip to content

Instantly share code, notes, and snippets.

@Shagshag
Created October 4, 2018 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shagshag/5db8dd200fe1c36a555ff8a8e67dc981 to your computer and use it in GitHub Desktop.
Save Shagshag/5db8dd200fe1c36a555ff8a8e67dc981 to your computer and use it in GitHub Desktop.
Delete all files and subfolders in the current folder
<?php
/**
* Delete all files and subfolders in the current folder
* I use it to start a new project
* Be careful : There is no confirmation, this script deletes everything it can, including itself
*/
// https://stackoverflow.com/a/17161106/2530962 php glob - scan in subfolders for a file
function rglob($pattern, $flags = 0) {
$files = (array) glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
}
return $files;
}
// https://stackoverflow.com/a/33059445/2530962 Get all file all subfolders and all hidden file with glob
// https://stackoverflow.com/a/13468943/2530962 Deleting all files from a folder using PHP?
// this line returns warnings because unlink can't delete folders. There will be deleted after
array_map('unlink', rglob(dirname(__FILE__)."/{,.}[!.,!..]*",GLOB_MARK|GLOB_BRACE));
$folders = array_reverse(rglob(dirname(__FILE__)."/{,.}[!.,!..]*",GLOB_MARK|GLOB_BRACE));
array_map('rmdir', $folders);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment