The rmdir()
function fails if the folder contains any files. I can loop through all of the the files in the directory with something like this:
foreach (scandir($dir) as $item) { if ($item == '.' || $item == '..') continue; unlink($dir.DIRECTORY_SEPARATOR.$item); } rmdir($dir);
Is there any way to just delete it all at once?
To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.
If you want to delete everything from folder (including subfolders) use this combination of array_map , unlink and glob : array_map( 'unlink', array_filter((array) glob("path/to/temp/*") ) );
To remove a directory that is not empty, use the rm command with the -r option for recursive deletion.
rrmdir()
-- recursively delete directories:
function rrmdir($dir) { foreach(glob($dir . '/*') as $file) { if(is_dir($file)) rrmdir($file); else unlink($file); } rmdir($dir); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With