mcmuney Posted January 17, 2008 Share Posted January 17, 2008 Hey, I'm try to delete a directory, but I get, "Directory not empty" error. How can I delete the directory and it's content? I'm using this code: rmdir($paththumbs); Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/ Share on other sites More sharing options...
teng84 Posted January 17, 2008 Share Posted January 17, 2008 <? if ($handle = opendir('admin')) { while (false !== ($file = readdir($handle))) { unlink($file); } closedir($handle); } rmdir($paththumbs); ?> Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/#findComment-441481 Share on other sites More sharing options...
mcmuney Posted January 17, 2008 Author Share Posted January 17, 2008 I modified the 1st line and then attempted it and this time I got many more errors, am I missing something? <? if ($handle = opendir($paththumbs)) { while (false !== ($file = readdir($handle))) { unlink($file); } closedir($handle); } rmdir($paththumbs); ?> Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/#findComment-441488 Share on other sites More sharing options...
teng84 Posted January 17, 2008 Share Posted January 17, 2008 what is the error? please post the error so we know where you went wrong Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/#findComment-441492 Share on other sites More sharing options...
mcmuney Posted January 17, 2008 Author Share Posted January 17, 2008 Below is the error print-screen and for your reference, line 62 is unlink($file); and line 66 is rmdir($paththumbs): Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/#findComment-441553 Share on other sites More sharing options...
teng84 Posted January 17, 2008 Share Posted January 17, 2008 that means you still have dir inside your dir .. hmm let me play on this again.. Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/#findComment-441556 Share on other sites More sharing options...
mcmuney Posted January 17, 2008 Author Share Posted January 17, 2008 I don't have another directory within the directory, this is the path below. I basically want to delete the /thumbs folder and anything inside it (there are no folders under /thumbs, only files): $paththumbs = "../photos/".$rowPtFila[date]."/thumbs"; Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/#findComment-441563 Share on other sites More sharing options...
teng84 Posted January 17, 2008 Share Posted January 17, 2008 try to echo your files under that dir <? if ($handle = opendir($paththumbs)) { while (false !== ($file = readdir($handle))) { if($file == '.'|| $file == '..'){echo 'this is not directoty<br/>';} else { echo $file.'<br/>';} } closedir($handle); } ?> try and see what it returns or maybe this <? if ($handle = opendir($paththumbs)) { while (false !== ($file = readdir($handle))) { unlink($paththumbs.'/'.$file); } closedir($handle); } rmdir($paththumbs); ?> Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/#findComment-441579 Share on other sites More sharing options...
mcmuney Posted January 17, 2008 Author Share Posted January 17, 2008 Ok, some progress made here. The first code didn't work and resulted in this error: The second code also produced an error, but managed to delete the folder and its contents. Now, just need to get rid of the error: Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/#findComment-441645 Share on other sites More sharing options...
priti Posted January 17, 2008 Share Posted January 17, 2008 Hi, How about using this $cmd= 'rm -rf <directoryName>' ; exec($cmd); regards Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/#findComment-441651 Share on other sites More sharing options...
mcmuney Posted January 17, 2008 Author Share Posted January 17, 2008 Can you elaborate on that a bit more? I'm not sure how to incorporate that into the existing code. Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/#findComment-442078 Share on other sites More sharing options...
priti Posted January 18, 2008 Share Posted January 18, 2008 Can you elaborate on that a bit more? I'm not sure how to incorporate that into the existing code. write below set of code replacing <directoryName> where you are writing your present code.Comment that first and try this two lines of code. $cmd= 'rm -rf <directoryName>' ; This command will delete the directory even if it contains the content in it. exec($cmd); Regards Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/#findComment-442445 Share on other sites More sharing options...
mcmuney Posted January 18, 2008 Author Share Posted January 18, 2008 It didn't work and it didn't give me any errors either. if(is_dir($paththumbs)){ $cmd= 'rm -rf <$paththumbs>' ; exec($cmd); } Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/#findComment-442592 Share on other sites More sharing options...
ratcateme Posted January 18, 2008 Share Posted January 18, 2008 Got this from http://nz.php.net/unlink 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($paththumbs, true) ; Scott. Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/#findComment-442626 Share on other sites More sharing options...
priti Posted January 21, 2008 Share Posted January 21, 2008 It didn't work and it didn't give me any errors either. if(is_dir($paththumbs)){ $cmd= 'rm -rf <$paththumbs>' ; exec($cmd); } correction in code $cmd= 'rm -rf $paththumbs' ; Now try, Regards Link to comment https://forums.phpfreaks.com/topic/86395-directory-and-content-deletion/#findComment-444787 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.