Fix for Fix for Image Field Import

  1. function cp_field_file_save_file($filepath, $validators = array(), $dest = FALSE, $account = NULL) {
  2.  
  3.   // Add in our check of the the file name length.
  4.   $validators['file_validate_name_length'] = array();
  5.  
  6.   // Begin building file object.
  7.   $file = new stdClass();
  8.   $file->uid = $account;
  9.   $file->filename = basename($filepath);
  10.   $file->filepath = $filepath;
  11.  
  12.   $file->source = 'field_file_save_file';
  13.   $file->destination = file_destination(file_create_path($dest .'/'. $file->filename), FILE_EXISTS_REPLACE);
  14.   $file->filesize = filesize($filepath);
  15.  
  16.   if (!file_copy($file, $file->destination, FILE_EXISTS_REPLACE)) {
  17.     form_set_error($file->source, t('File upload error. Could not move uploaded file.'));
  18.     watchdog('file', 'Upload error. Could not move file %file to destination %destination.', array('%file' => $file->filename, '%destination' => $file->destination));
  19.     return 0;
  20.   }
  21.  
  22.   // If we made it this far it's safe to record this file in the database.
  23.   $file->status = FILE_STATUS_PERMANENT;
  24.   $file->timestamp = time();
  25.   // Insert new record to the database.
  26.   drupal_write_record('files', $file);
  27.  
  28.   _field_file_cache($file); // cache the file in order to minimize load queries
  29.   return (array)$file;
  30. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.