Jump to content

Recommended Posts

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 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);
            }
           }
       }
   }
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/10071-deleting-folders-in-php/
Share on other sites

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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.