You are here

function uc_file_remove_by_id in Ubercart 6.2

Same name and namespace in other branches
  1. 8.4 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

$fid: An Ubercart file id.

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

Return value

A boolean stating whether or not all requested operations succeeded.

1 call to uc_file_remove_by_id()
uc_file_admin_files_form_action_submit in uc_file/uc_file.admin.inc
Submit handler for uc_file_admin_files_form().

File

uc_file/uc_file.module, line 1360

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.
  $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 = db_result(db_query("SELECT filename FROM {uc_files} WHERE fid = %d", $fid));
    $dir = uc_file_qualify_file($filename);
    if (is_dir($dir)) {

      // Only if it's empty.
      $dir_contents = file_scan_directory($dir, '.*', array(
        '.',
        '..',
        'CVS',
      ), 0, FALSE);
      if (empty($dir_contents)) {
        if (rmdir($dir)) {
          drupal_set_message(t('The directory %dir was deleted.', array(
            '%dir' => $filename,
          )));
          $remove_fields = TRUE;
        }
        else {
          drupal_set_message(t('The directory %dir could not be deleted.', array(
            '%dir' => $filename,
          )));
          $result = FALSE;
        }
      }
      else {
        drupal_set_message(t('The directory %dir could not be deleted because it is not empty.', array(
          '%dir' => $filename,
        )));
        $result = FALSE;
      }
    }
    else {
      if (unlink($dir)) {
        $remove_fields = TRUE;
        drupal_set_message(t('The file %dir was deleted.', array(
          '%dir' => $filename,
        )));
      }
      else {
        drupal_set_message(t('The file %dir could not be deleted.', array(
          '%dir' => $filename,
        )));
        $result = FALSE;
      }
    }

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