You are here

function filefield_file_insert in FileField 5.2

Same name and namespace in other branches
  1. 5 filefield.module \filefield_file_insert()

insert a file into the database.

Parameters

$node: node object file will be associated with.

$file: file to be inserted, passed by reference since fid should be attached.

2 calls to filefield_file_insert()
filefield_field in ./filefield.module
Implementation of hook_field().
filefield_file_update in ./filefield.module
update the file record if necessary

File

./filefield.module, line 151
Defines a file field type.

Code

function filefield_file_insert($node, $field, &$file) {
  $fieldname = $field['field_name'];

  // allow tokenized paths.
  if (function_exists('token_replace')) {
    global $user;
    $widget_file_path = token_replace($field['widget']['file_path'], 'user', $user);
  }
  else {
    $widget_file_path = $field['widget']['file_path'];
  }
  if (filefield_check_directory($widget_file_path)) {
    $filepath = file_create_path($widget_file_path) . '/' . $file['filename'];
    if ($file = file_save_upload((object) $file, $filepath)) {
      $file = (array) $file;
      $file['fid'] = db_next_id('{files}_fid');
      $file['filemime'] = mimedetect_mime($file);
      db_query("INSERT into {files} (fid, nid, filename, filepath, filemime, filesize)\n                VALUES (%d, %d, '%s','%s','%s',%d)", $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']);
      module_invoke_all('filefield', 'file_save', $node, $field, $file);
      return (array) $file;
    }
    else {

      // Include file name in upload error.
      form_set_error(NULL, t('File upload was unsuccessful.'));
      return FALSE;
    }
  }
  else {

    // Include file name in upload error.
    form_set_error(NULL, t('File upload was unsuccessful.'));
    return FALSE;
  }
}