You are here

function webfm_dbinsert_file in Web File Manager 5.2

Same name and namespace in other branches
  1. 5 webfm.module \webfm_dbinsert_file()

webfm_dbinsert_file - inserts a file object into the webfm_file table

Parameters

object $file - complete file object:

string $path - a string containing the path relative to drupal root:

array $metadata - an array of key => value pairs, where key matches a field in the webfm_file table:

Return value

bool - TRUE if query executed successfully, otherwise FALSE

3 calls to webfm_dbinsert_file()
webfm_ajax in ./webfm.module
Ajax post requests
webfm_insert_file in ./webfm_file.inc
webfm_insert_file - inserts a file into the webfm_file table
webfm_upload in ./webfm.module
Called by upload form submit

File

./webfm.module, line 1826

Code

function webfm_dbinsert_file($file, &$error, $metadata = array()) {

  //we need our user
  global $user;

  //add additional values to $metadata
  unset($metadata['fid']);
  $metadata['uid'] = $user->uid;
  $metadata['fpath'] = $file->filepath;
  $metadata['fname'] = strrev(substr(strrev($file->filepath), 0, strpos(strrev($file->filepath), '/')));
  $metadata['fsize'] = $file->filesize ? $file->filesize : filesize($file->filepath);
  $metadata['fcreatedate'] = @filemtime($file->filepath);

  // Get file extension
  if ($file->filemime) {
    $metadata['fmime'] = $file->filemime;
  }
  else {
    if ($pos = strrpos($file->filepath, '.')) {
      $ext = substr($file->filepath, $pos + 1);
    }
    else {
      $ext = '';
    }
    $metadata['fmime'] = $ext;
  }
  $metadata['fdesc'] = $file->fdesc ? $file->fdesc : '';

  //create a string of fields for the query
  $fields = implode(', ', array_keys($metadata));

  //build printf style list of values
  foreach ($metadata as $key => $value) {
    if (is_numeric($value)) {
      $printfvalues[] = '%d';
    }
    else {
      $printfvalues[] = "'%s'";
    }
  }

  //create a srting of printf style values
  $printfvalues = implode(', ', $printfvalues);

  //create an array of just the values for the db_query
  $values = array_values($metadata);

  //make a db_query friendly query with prinf stuff
  $query = "INSERT INTO {webfm_file} ({$fields}) VALUES ({$printfvalues})";
  $result = db_query($query, $values);
  if ($result === FALSE) {
    $error = $file->filepath . ' could not be inserted into db';
    drupal_set_message(t('Could not insert %file into the database', array(
      '%file' => $file->filepath,
    )), error);
    return FALSE;
  }
  else {
    return TRUE;
  }
}