lindylex Posted May 14, 2008 Share Posted May 14, 2008 This is a simple delete all files in a directory and then delete the directory but it does not work. Does any one have an idea? function del_this_directory ($directory_to_del){ foreach (new DirectoryIterator ($directory_to_del) as $file){ if (!is_dir ($file)){ unlink ($file ->getPathname ()); } } //This will remove the directory rmdir($directory_to_del); } del_this_directory ("../del/"); This will delete the directory if I do this without obviously no file(s) within the directory. function del_this_directory ($directory_to_del){ /* foreach (new DirectoryIterator ($directory_to_del) as $file){ if (!is_dir ($file)){ unlink ($file ->getPathname ()); } } */ //This will remove the directory rmdir($directory_to_del); } del_this_directory ("../del/"); I think something here "foreach (new DirectoryIterator ($directory_to_del) as $file){" is keeping the directory open. An not letting me delete the empty directory. Just to let you know the permission for this directory is 0777 so that's not the problem. Thanks Link to comment https://forums.phpfreaks.com/topic/105549-delete-all-files-in-a-directory-and-then-delete-the-directory/ Share on other sites More sharing options...
MadTechie Posted May 14, 2008 Share Posted May 14, 2008 try this (its not mine but works well) <?php // $dir = the target directory // $DeleteMe = if true delete also $dir, if false leave it alone function SureRemoveDir($dir, $DeleteMe) { if(!$dh = @opendir($dir)) return; while (false !== ($obj = readdir($dh))) { if($obj=='.' || $obj=='..') continue; if (!@unlink($dir.'/'.$obj)) SureRemoveDir($dir.'/'.$obj, true); } closedir($dh); if ($DeleteMe){ @rmdir($dir); } } //SureRemoveDir('EmptyMe', false); //SureRemoveDir('RemoveMe', true); ?> Link to comment https://forums.phpfreaks.com/topic/105549-delete-all-files-in-a-directory-and-then-delete-the-directory/#findComment-540775 Share on other sites More sharing options...
Rohan Shenoy Posted May 14, 2008 Share Posted May 14, 2008 I don't know exactly what you need but won't deleting the directory itself delete all the files and foldeers under it? Why to delete each one individually? Link to comment https://forums.phpfreaks.com/topic/105549-delete-all-files-in-a-directory-and-then-delete-the-directory/#findComment-540842 Share on other sites More sharing options...
lindylex Posted May 14, 2008 Author Share Posted May 14, 2008 Rohan Shenoy, have ever tried to point "rmdir("path/to/a/nonemptydriectory")". If you try it you will notice that you have to remove all the files within it first. Lex Link to comment https://forums.phpfreaks.com/topic/105549-delete-all-files-in-a-directory-and-then-delete-the-directory/#findComment-541351 Share on other sites More sharing options...
lindylex Posted May 16, 2008 Author Share Posted May 16, 2008 MadTechie, thanks for this solution. I was trying to use that class DirectoryIterator built for PHP 5. I will experiment later and share my discoveries. Thanks, Lex Link to comment https://forums.phpfreaks.com/topic/105549-delete-all-files-in-a-directory-and-then-delete-the-directory/#findComment-542526 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.