PHP: safe way to delete a directory?

Consider this code:

public static function removeDir($src)
{
    if (is_dir($src)) {
        $dir = @opendir($src);
        if ($dir === false)
            return;
        while(($file = readdir($dir)) !== false) {
            if ($file != '.' && $file != '..') {
                $path = $src . DIRECTORY_SEPARATOR . $file;
                if (is_dir($path)) {
                    self::removeDir($path);
                } else {
                    @unlink($path);
                }
            }
        }
        closedir($dir);
        @rmdir($src);
    }
}

This will delete the directory. But if unlink does not work or opendir does not work in any subdirectory, the directory will be left with some content.

I want either everything to be deleted or nothing to be deleted. I am thinking of copying the directory before deleting, and if something fails, restoring the copy. But maybe there is a better way - for example, blocking files or something like that?

+4
source share
2 answers

I am a general, I would confirm the comment:

"Copy it, delete it, copy it back if it deleted again to delete the error message ..." - We0

:

save save , . "" . , php - testdelete, , , ( , - ). :

  • , . . MySQL/InnoDB
  • " /". , A B , php A, B. php A B. , , , . , , , , ..
  • . . , , , , .

, , :) - , - :

  • , , , ( )
  • ,
  • , (- ):
    • ( , )
  • , / ;
  • ( !)

. , - 5 6.

+1

/ - /tmp/, , . , . , tmp.

0

All Articles