0

I'm trying to have my code delete a temporary directory that was generated earlier in the code. This directory contains multiple files and subdirectories, all of which are recursively removed before mkdir is called. however, rmdir generates an arror, claiming the directory isn't empty.

Scandir returns three values: ".", ".." and a generic folder that should've been removed earlier. However, file_exists returns false on this folder.
I have checked for hidden folders/files and I have checked permissions, but the error still occurs. Any ideas what might be causing this?

Code:

$clean=array();
foreach($files as $file){       // $files is a RecursiveIteratorIterator that worked fine earlier in the script
    array_push($clean,$file);
}
// sort files by name length to start with the subdirectories and work towards the root
usort($clean,function($a,$b){
    return strlen($b)-strlen($a);
});
foreach($clean as $file){
    if(!is_dir($file)&&!preg_match('/\.{1,2}$/',$file)){
        unlink($file);
    }elseif(preg_match('/\.{2}$/',$file)){
        rmdir(str_replace('\..','',$file));
    }
}
rmdir($tmpdir);

This script is in my root folder.
The directory to be removed is: [root]\files\AV[generic name]
$tmpdir is the path to this directory and it works fine earlier in the code.
e.g. if the folder to be removed is [root]\files\AV\tmp then $tmpdir='files/AV/tmp/'

  • 1
    Please check this link:-http://stackoverflow.com/questions/1653771/how-do-i-remove-a-directory-that-is-not-empty. nice example and explanation is given – Alive to die - Anant Apr 13 '15 at 08:29
  • Well, if there is some folder in there and it is not deleted then certainly the error makes sense. So your question should be why that folder in there is not removed... – arkascha Apr 13 '15 at 08:35
  • @anant kumar singh Thanks, but I've alredy tried that. I get the same error message: "rmdir(files/AV/tmp-1428914073/): Directory not empty" – Grimnis Hvern Apr 13 '15 at 08:35
  • please edit your code.it's not proper and also show an screen shot of your folder structure on which you are applying this. first edit code. it's look really bad. previously it looks good. – Alive to die - Anant Apr 13 '15 at 08:39
  • @arkascha I get mixed massages. rmdir claims $tmpdir isn't empty and scandir finds a folder, but file_exists on this folder returns false and the folder is not there in the explorer. – Grimnis Hvern Apr 13 '15 at 08:39
  • Folder screen shot if possible on which you are going to call this function – Alive to die - Anant Apr 13 '15 at 08:45
  • is delete possible if manually delete them? – Tharif Apr 13 '15 at 08:47
  • Sorry, I'm too new to Stackoverflow to post images :( @utility Yes it is. – Grimnis Hvern Apr 13 '15 at 08:49
  • What kind of mysterious folder is that? How is it called? – arkascha Apr 13 '15 at 09:01
  • @arkasha Kind of a long story. I need a script that uploads a zip archive and stores the content on the server, but I also need to keep te zip file. in addition, the structure of the zip archive's needs to be checked and possibli altered. This script uploads a zip file, creates a folder named tmp-[timestamp] and extracts the contents of the zip file there. The structure is checked and the proper actions are performed. then, the temporary folder must bedeleted, together with all its contents. That's what I'm trying to accomplish here. – Grimnis Hvern Apr 13 '15 at 09:19

0 Answers0