holowugz Posted May 20, 2006 Share Posted May 20, 2006 Hi this little snippet of code when run should delete all the folders and the files in them from the directory msn_files except a directory which is called what ever the current date is.and it is just not working, the error i get is:Warning: rmdir(msn_files/20-05-2006): Directory not empty in (Line 108)which is all very well and good but my if statement should be stopping it from trying to delte the directory marked with whatever the date is, would really appreciate some fresh eyes [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] [code]//directory name of directory to delte form$dir ="msn_files";//directory we want to protect$currentDir = $dir . "/" . date('d-m-Y');//if the directory exists procedeif (file_exists($dir)) {//select all the folders and files in the main directory $arrayFiles = glob("$dir/*");//if there are folders and files in the directory proceed if($arrayFiles){ foreach($arrayFiles as $filename) {//if the name of the file we are looking at is not the protected directory or called .htaccess then proceed if($filename != $currentDir or $filename != $dir .'.htaccess'){if it is a directory then proceed if(is_dir($filename)){//select all the files in the new directtory $arrayFiles2 = glob("$dir/$filename/*"); if($arrayFiles2){ foreach($arrayFile2s as $filename2) {//set them to 0777 so we can deltee them chmod($filename2,0777);//delete the files unlink($filename2); } }//make sure we dont delte the protected folder if($filename != $currentDir or $filename != $dir .'.htaccess'){delete the folder now we emptied it rmdir($filename); } }else{//if it is not a folder then delete the file unlink($filename); } } } }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10071-deleting-folders-in-php/ Share on other sites More sharing options...
holowugz Posted May 20, 2006 Author Share Posted May 20, 2006 hi i got it to work now, thought i would post the finished code:[code]$dir ="msn_files";$currentDir = $dir . "/" . date('d-m-Y');if (file_exists($dir)) { $arrayFiles = glob("$dir/*"); if($arrayFiles){ foreach($arrayFiles as $filename) { if($filename != $currentDir and $filename != $dir .'.htaccess'){ if(is_dir($filename)){ $arrayFiles2 = glob("$filename/*"); if($arrayFiles2){ foreach($arrayFiles2 as $filename2) { chmod($filename2,0777); unlink($filename2); } } if($filename != $currentDir and $filename != $dir .'.htaccess'){ rmdir($filename); } }else{ unlink($filename); } } } }}[/code]Luke Brown Quote Link to comment https://forums.phpfreaks.com/topic/10071-deleting-folders-in-php/#findComment-37463 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.