Jump to content


Photo

Deleting folders in PHP


  • Please log in to reply
1 reply to this topic

#1 holowugz

holowugz
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 20 May 2006 - 05:56 PM

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\" /]



//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 procede
if (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);
            }
           }
       }
   }
}


#2 holowugz

holowugz
  • Members
  • PipPipPip
  • Advanced Member
  • 46 posts

Posted 20 May 2006 - 06:28 PM

hi i got it to work now, thought i would post the finished 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);
            }
           }
       }
   }
}

Luke Brown




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users