0

I'm writing a json-file inside a generated folder. After an hour I want to delete the folder with its content automatically. I tried:

$dir = "../tmpDir";
    $cdir = scandir($dir);
    foreach ($cdir as $key => $value)
   {
      if (!in_array($value,array(".","..")))
      {
         if (is_dir($dir.'/'.$value))
         {
             if(filectime($dir.'/'.$value)< (time()-3600))
             {  // after 1 hour
                $files = glob($dir.'/'.$value); // get all file names
                foreach($files as $file)
                { // iterate files
                    if(is_file($file))
                    {
                        unlink($file); // delete file
                    }
                }
                rmdir($dir.'/'.$value);


                /*destroy the session if the folder is deleted*/

                if(isset($_SESSION["dirname"]) && $_SESSION["dirname"] == $value)
                {
                    session_unset();     // unset $_SESSION variable for the run-time 
                    session_destroy();   // destroy session data in storage
                }
             }
         }
      }
   }

I get: rmdir(../tmpDir/1488268867): Directory not empty in /Applications/MAMP/htdocs/.... on line 46

if I remove the

if(is_file($file))
{

}

I get a permission error

Maybe someone knows why I get this error

1

2 Answers 2

3

It's much easier to use your native O/S to delete the directory when it comes to things like these, so you don't have to write a horrible loop that may have some edge cases that you could miss and then end up deleting things you shouldn't have!

$path = 'your/path/here';

if (PHP_OS !== 'WINDOWS')
{
    exec(sprintf('rm -rf %s', $path));
}
else
{
    exec(sprintf('rd /s /q %s', $path));
}

Of course, tailor the above to your needs. You can also use the backtick operator if you want to avoid the overhead of a function call (negligible in this case, anyway). It's also probably a good idea to use escape_shell_arg for your $path variable.

For a one-liner:

exec(sprintf((PHP_OS === 'WINDOWS') ? 'rd /s /q %s' : 'rm -rf %s', escape_shell_arg($path)));

Regardless, sometimes it's easier to let the O/S of choice perform file operations.

Sign up to request clarification or add additional context in comments.

Comments

3

rmdir() removes directory if u want remove file then you should use unlink() function

Proper aporach would be using DirectoryIterator or glob() and looping through files then deleting them and after you do it you can remove empty directory.

You can also call system command rm -rf direcory_name with exec() or shell_exec()

useful links:

I've found pretty useful function on php.net that removes hidden files as well

 public function delTree($dir) { 
    $files = array_diff(scandir($dir), array('.','..')); 

    foreach ($files as $file) { 
      (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file"); 
    } 

    return rmdir($dir); 
  }

3 Comments

I want to delete the folder with its content
Thank you this is really helpfull, but somehow the is_file() doesn't fire on my .json files - so I guess my only option is to use system commands
check if this file has proper chmods

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.