N98\Util\Filesystem::recursiveRemoveDirectory PHP Method

recursiveRemoveDirectory() public method

See also: http://lixlpixel.org/recursive_function/php/recursive_directory_delete/
public recursiveRemoveDirectory ( string $directory, boolean $empty = false ) : boolean
$directory string
$empty boolean
return boolean
    public function recursiveRemoveDirectory($directory, $empty = false)
    {
        // if the path has a slash at the end we remove it here
        if (substr($directory, -1) === '/') {
            $directory = substr($directory, 0, -1);
        }
        // if the path is not valid or is not a directory ...
        // ... if the path is not readable
        if (!is_dir($directory) || !is_readable($directory)) {
            return false;
        }
        // we open the directory
        $handle = opendir($directory);
        if (!$handle) {
            throw new RuntimeException(sprintf('Directory <%s> error', $directory));
        }
        $skip = array(".", "..");
        // and scan through the items inside
        while (false !== ($file = readdir($handle))) {
            // if the filepointer is not the current directory
            // or the parent directory
            if (in_array($file, $skip)) {
                continue;
            }
            // we build the new path to delete
            $path = $directory . '/' . $file;
            // if the new path is a directory
            // don't recursively delete symlinks - just remove the actual link
            // this is helpful for extensions sym-linked from vendor directory
            // previous behaviour would wipe out the files in the vendor directory
            if (!is_link($path) && is_dir($path)) {
                // we call this function with the new path
                $this->recursiveRemoveDirectory($path);
                // if the new path is a file
            } else {
                // we remove the file
                unlink($path);
            }
        }
        closedir($handle);
        // if the option not empty
        if (!$empty) {
            return rmdir($directory);
        }
        // return success
        return true;
    }

Usage Example

Example #1
0
 /**
  * @param string $path
  *
  * @return bool
  */
 private function emptyDirectory($path)
 {
     $errors = array();
     $dir = new FilesystemIterator($path);
     foreach ($dir as $file => $info) {
         if ($info->isDir()) {
             $this->verbose('<debug>Filesystem::recursiveRemoveDirectory() <comment>' . $file . '</comment></debug>');
             if (!isset($fs)) {
                 $fs = new Filesystem();
             }
             if (!$fs->recursiveRemoveDirectory($file)) {
                 $errors[] = $file;
             }
         } else {
             $this->verbose('<debug>unlink() <comment>' . $file . '</comment></debug>');
             if (!unlink($file)) {
                 $errors[] = $file;
             }
         }
     }
     if (!$errors) {
         return true;
     }
     $message = sprintf("Failed to empty directory %s, unable to remove:\n", var_export($path, true));
     foreach ($errors as $error) {
         $message .= sprintf(" - %s\n", var_export($error, true));
     }
     throw new RuntimeException($message);
 }
All Usage Examples Of N98\Util\Filesystem::recursiveRemoveDirectory