You are here

function _auditfiles_not_in_database_batch_add_process_file in Audit Files 7.3

Same name and namespace in other branches
  1. 7.4 auditfiles.notindatabase.inc \_auditfiles_not_in_database_batch_add_process_file()

Adds the specified file to the database.

Parameters

string $filepathname: The full pathname to the file to add to the database.

1 call to _auditfiles_not_in_database_batch_add_process_file()
_auditfiles_not_in_database_batch_add_process_batch in ./auditfiles.notindatabase.inc
The batch process for adding the file.

File

./auditfiles.notindatabase.inc, line 572
Generates a report showing files on the server, but not in the database.

Code

function _auditfiles_not_in_database_batch_add_process_file($filepathname) {
  global $user;
  $file = new stdClass();
  $file->uid = $user->uid;
  $file->filename = trim(basename($filepathname));
  $file->uri = file_build_uri($filepathname);

  // Get the full system file path name, so that the following two operations
  // will work.
  $real_filenamepath = drupal_realpath($file->uri);
  $file->filemime = file_get_mimetype($real_filenamepath);
  $file->filesize = filesize($real_filenamepath);
  $file->status = FILE_STATUS_PERMANENT;
  $file->timestamp = REQUEST_TIME;

  // Make sure the file is not already in the database.
  $query = 'SELECT fid FROM {file_managed} WHERE uri = :uri';
  $existing_file = db_query($query, array(
    ':uri' => $file->uri,
  ))
    ->fetchAll();
  if (empty($existing_file)) {

    // The file is not already in the database, so add it.
    $results = drupal_write_record('file_managed', $file);
    if (empty($results)) {
      drupal_set_message(t('Failed to add %file to the database.', array(
        '%file' => $filepathname,
      )));
    }
    else {
      drupal_set_message(t('Sucessfully added %file to the database.', array(
        '%file' => $filepathname,
      )));

      // Remove the file from the list of files to display.
      $rows = variable_get('auditfiles_not_in_database_files_to_display', array());
      unset($rows[$filepathname]);
      variable_set('auditfiles_not_in_database_files_to_display', $rows);
    }
  }
  else {
    drupal_set_message(t('The file %file is already in the database.', array(
      '%file' => $filepathname,
    )), 'error');
  }
}