Created
October 4, 2018 08:25
-
-
Save Shagshag/5db8dd200fe1c36a555ff8a8e67dc981 to your computer and use it in GitHub Desktop.
Delete all files and subfolders in the current folder
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 | |
/** | |
* 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