Jump to content

remove file and directory


christa

Recommended Posts

I must to delete before all the files in directories and after the directories.

 

I use this code but it doesn't works: why?

 

if ($handle = opendir("../$file_dir")) {
  while (false !== ($file = readdir($handle))) {
    
    if ($file != "." && $file != "..") {
        
       if(file_exists($file) && is_file($file)) {
             unlink($file);
         }elseif(is_dir($file)){
             $handle = opendir($file);
              while (false !== ($files = readdir($handle))) { 
                 if(is_file($file.$files)){
                     unlink($file.$files);
                 }
          }
        $handle = closedir($handle);
        rmdir($file);
  }
}

}
closedir($handle);
}

Link to comment
Share on other sites

So the path to the directory that you want to remove is ../../upload, is that correct? If that path is correct and the problem lies somewhere else I'd suggest putting in debugging echos in varies parts of the code to see exactly what is and what isn't being executed, from that you should be able to determine what's going wrong.

Link to comment
Share on other sites

now, i added else at the end: the script go to else printing the directories names!.

 

if ($handle = opendir("../$file_dir")) {
  while (false !== ($file = readdir($handle))) {
   
    if ($file != "." && $file != "..") {
       
       if(file_exists($file) && is_file($file)) {
             unlink($file);
         }elseif(is_dir($file)){
             $handle = opendir($file);
              while (false !== ($files = readdir($handle))) {
                 if(is_file($file.$files)){
                     unlink($file.$files);
                 }
          }
        $handle = closedir($handle);
        rmdir($file);
 } else { 
        echo $file ."<br>"; 
       }
  }
}

}
closedir($handle);

 

Link to comment
Share on other sites

Im not too sure if this would be the best and easiest way.

If the directories are placed under a database, wouldn't it be easier to delete all the rows using a mysql query or by deleting the row of the logged in user?

the directories are not placed under a database.

I don't understand why nor file nor directories are deleted

Link to comment
Share on other sites

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.