You are here

public static function H5PStorageImport::deleteFileTree in Opigno module 8

Same name and namespace in other branches
  1. 3.x src/H5PImportClasses/H5PStorageImport.php \Drupal\opigno_module\H5PImportClasses\H5PStorageImport::deleteFileTree()

Recursive function for removing directories.

Parameters

string $dir: Path to the directory we'll be deleting.

Return value

bool Indicates if the directory existed.

2 calls to H5PStorageImport::deleteFileTree()
H5PStorageImport::saveLibraries in src/H5PImportClasses/H5PStorageImport.php
Helps savePackage.
H5PValidatorImport::isValidPackage in src/H5PImportClasses/H5PValidatorImport.php
Validates a .h5p file.

File

src/H5PImportClasses/H5PStorageImport.php, line 164

Class

H5PStorageImport
This class is used for saving H5P files.

Namespace

Drupal\opigno_module\H5PImportClasses

Code

public static function deleteFileTree($dir) {
  if (!is_dir($dir)) {
    return FALSE;
  }
  if (is_link($dir)) {

    // Do not traverse and delete linked content, simply unlink.
    unlink($dir);
    return TRUE;
  }
  $files = array_diff(scandir($dir), [
    '.',
    '..',
  ]);
  foreach ($files as $file) {
    $filepath = "{$dir}/{$file}";

    // Note that links may resolve as directories.
    if (!is_dir($filepath) || is_link($filepath)) {

      // Unlink files and links.
      unlink($filepath);
    }
    else {

      // Traverse subdir and delete files.
      self::deleteFileTree($filepath);
    }
  }
  return rmdir($dir);
}