coolphpdude Posted June 23, 2008 Share Posted June 23, 2008 Hello there, I've been trying to delete a folder for a particular user. I found that you can't delete the folder unless it is empty. So i have been trying to look for ways to loop through deleting the content then removing the folder. so far i've got the following... (and it doesnt work so im stuck!) function deltree( $f ){ if( is_dir( $f ) ){ foreach( scandir( $f ) as $item ){ if( !strcmp( $item, '.' ) || !strcmp( $item, '..' ) ) continue; deltree( $f . "/" . $item ); } rmdir( $f ); } else{ unlink( $f ); } } Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/ Share on other sites More sharing options...
MadTechie Posted June 23, 2008 Share Posted June 23, 2008 try this <?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/111493-delete-folder-and-its-contents/#findComment-572196 Share on other sites More sharing options...
coolphpdude Posted June 23, 2008 Author Share Posted June 23, 2008 i'll give that a try, two mins Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572220 Share on other sites More sharing options...
coolphpdude Posted June 23, 2008 Author Share Posted June 23, 2008 hmmm, nope, its not giving me an error or anything but its not deleting the contents of my folder either... any ideas? i've obviously set the $dir variable Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572233 Share on other sites More sharing options...
.josh Posted June 23, 2008 Share Posted June 23, 2008 chmod permissions on files/folder set to allow it? and a dumb question: you did remove the //'s from the function calls at the bottom and put your targets in the arguments, right? Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572238 Share on other sites More sharing options...
coolphpdude Posted June 23, 2008 Author Share Posted June 23, 2008 $dir = "/testfolder/test/"; echo "$dir"; // $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); is that right? I think the permissions are all set, theres no errors coming up about permission denied. Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572245 Share on other sites More sharing options...
MadTechie Posted June 23, 2008 Share Posted June 23, 2008 remove the example from the bottom REMOVE SureRemoveDir('EmptyMe', false); SureRemoveDir('RemoveMe', true); Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572247 Share on other sites More sharing options...
.josh Posted June 23, 2008 Share Posted June 23, 2008 no don't remove it, because then you aren't calling your function. You need to pass it the right arguments, not just "emptyme" Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572250 Share on other sites More sharing options...
coolphpdude Posted June 23, 2008 Author Share Posted June 23, 2008 $dir = "/testfolder/test"; echo "$dir"; // $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); } } got that now? still doesnt work Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572258 Share on other sites More sharing options...
.josh Posted June 23, 2008 Share Posted June 23, 2008 you're not actually calling the function. at the bottom add SureRemoveDir($dir, true); Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572259 Share on other sites More sharing options...
MadTechie Posted June 23, 2008 Share Posted June 23, 2008 I can be a twat sometimes LOL didn't even notice he wasn't calling it from the first post Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572262 Share on other sites More sharing options...
coolphpdude Posted June 23, 2008 Author Share Posted June 23, 2008 lol ahh 1 sec Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572264 Share on other sites More sharing options...
coolphpdude Posted June 23, 2008 Author Share Posted June 23, 2008 nope still not working!! no errors or anything, all it displays is the echo request that i set so i can make sure my path is in the variable. Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572266 Share on other sites More sharing options...
coolphpdude Posted June 23, 2008 Author Share Posted June 23, 2008 im just off home, i'll pick up your replies when i get home. Cheers for your help lads Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572269 Share on other sites More sharing options...
.josh Posted June 23, 2008 Share Posted June 23, 2008 try taking off the leading / in $dir $dir = "testfolder/test"; Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572275 Share on other sites More sharing options...
MadTechie Posted June 23, 2008 Share Posted June 23, 2008 <?php $dir = "/testfolder/test"; //Remove the trailing / if(!file_exists($dir)) echo "Folder '$dir' doesn't exist"; echo "$dir"; SureRemoveDir($dir, true); // $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); } } ?> Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572283 Share on other sites More sharing options...
coolphpdude Posted June 23, 2008 Author Share Posted June 23, 2008 Its working, had to make a couple of modifications but that code and help you both gave me was a great help. i really appreciate it!! $dir = "testfolder/test/"; //echo "$dir"; //$dir = "/testfolder/test"; //Remove the trailing / if(!file_exists($dir)) echo "Folder '$dir' doesn't exist"; echo "$dir"; // $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($dir, true); I had to put SureRemoveDir($dir, true); after the code as it was trying to call the function before i had defined it. Also, $dir = "testfolder/test/";... i had to take off the / from before testfolder (i previously had $dir = "/testfolder/test/"; ) and it was saying the folder did not exist. Once i removed the beginning slash it worked a treat! Thanks again lads!! Link to comment https://forums.phpfreaks.com/topic/111493-delete-folder-and-its-contents/#findComment-572367 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.