You are here

function uc_file_remove_by_id in Ubercart 8.4

Same name and namespace in other branches
  1. 6.2 uc_file/uc_file.module \uc_file_remove_by_id()
  2. 7.3 uc_file/uc_file.module \uc_file_remove_by_id()

Deletes files (or directories).

First, the file IDs are gathered according to whether or not we're recurring. The list is sorted in descending file system order (i.e. directories come last) to ensure the directories are empty when we start deleting them. Checks are done to ensure directories are empty before deleting them. All return values from file I/O functions are evaluated, and if they fail (say, because of permissions), then db entries are untouched. However, if the given file/path is deleted correctly, then all associations with products, product features, and users will be deleted, as well as the uc_file db entries.

Parameters

int $fid: An Ubercart file id.

bool $recur: Whether or not all files below this (if it's a directory) should be deleted as well.

Return value

bool A boolean stating whether or not all requested operations succeeded.

2 calls to uc_file_remove_by_id()
ActionForm::submitForm in uc_file/src/Form/ActionForm.php
Form submission handler.
FileDeleteForm::submitForm in uc_file/src/Form/FileDeleteForm.php
Form submission handler.

File

uc_file/uc_file.module, line 498
Allows products to be associated with downloadable files.

Code

function uc_file_remove_by_id($fid, $recur) {

  // Store the overall status. Any fails will return FALSE through this.
  $result = TRUE;

  // Gather file(s) and sort in descending order. We do this
  // to ensure we don't try to remove a directory before it's empty.
  $connection = \Drupal::database();
  $fids = _uc_file_sort_fids(_uc_file_get_dir_file_ids($fid, $recur));
  foreach ($fids as $fid) {
    $remove_fields = FALSE;

    // Qualify the path for I/O, and delete the files/dirs.
    $filename = $connection
      ->query('SELECT filename FROM {uc_files} WHERE fid = :fid', [
      ':fid' => $fid,
    ])
      ->fetchField();
    $dir = uc_file_qualify_file($filename);
    if (is_dir($dir)) {

      // Only if it's empty.
      $dir_contents = file_scan_directory($dir, '/.*/', [
        'recurse' => FALSE,
      ]);
      if (empty($dir_contents)) {
        if (rmdir($dir)) {
          \Drupal::messenger()
            ->addMessage(t('The directory %dir was deleted.', [
            '%dir' => $filename,
          ]));
          $remove_fields = TRUE;
        }
        else {
          \Drupal::messenger()
            ->addWarning(t('The directory %dir could not be deleted.', [
            '%dir' => $filename,
          ]));
          $result = FALSE;
        }
      }
      else {
        \Drupal::messenger()
          ->addWarning(t('The directory %dir could not be deleted because it is not empty.', [
          '%dir' => $filename,
        ]));
        $result = FALSE;
      }
    }
    else {
      if (unlink($dir)) {
        $remove_fields = TRUE;
        \Drupal::messenger()
          ->addMessage(t('The file %dir was deleted.', [
          '%dir' => $filename,
        ]));
      }
      else {
        \Drupal::messenger()
          ->addError(t('The file %dir could not be deleted.', [
          '%dir' => $filename,
        ]));
        $result = FALSE;
      }
    }

    // Remove related tables.
    if ($remove_fields) {
      _uc_file_prune_db($fid);
    }
  }
  return $result;
}