File field not getting updated-Collection of common programming errors
Paniyari tried to update file cck field of a content type as below from another node as below.
file gets uploaded but results error in updating file field .
localhost Error The website encountered an unexpected error. Please try again later. Error message Notice: Undefined index: fid in file_field_presave() (line 219 of C:\xampp\htdocs\drupal\modules\file\file.field.inc). Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 173 of C:\xampp\htdocs\drupal\includes\entity.inc). Notice: Trying to get property of non-object in file_field_presave() (line 220 of C:\xampp\htdocs\drupal\modules\file\file.field.inc). Warning: Creating default object from empty value in file_field_presave() (line 221 of C:\xampp\htdocs\drupal\modules\file\file.field.inc). Notice: Undefined property: stdClass::$uri in file_save() (line 601 of C:\xampp\htdocs\drupal\includes\file.inc). Notice: Undefined index: fid in file_field_update() (line 261 of C:\xampp\htdocs\drupal\modules\file\file.field.inc). Notice: Undefined index: fid in file_field_update() (line 287 of C:\xampp\htdocs\drupal\modules\file\file.field.inc). Notice: Undefined property: stdClass::$fid in file_usage_add() (line 685 of C:\xampp\htdocs\drupal\includes\file.inc). PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'fid' cannot be null: INSERT INTO {file_usage} (fid, module, type, id, count) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4); Array ( [:db_insert_placeholder_0] => [:db_insert_placeholder_1] => file [:db_insert_placeholder_2] => node [:db_insert_placeholder_3] => 32 [:db_insert_placeholder_4] => 1 ) in file_usage_add() (line 692 of C:\xampp\htdocs\drupal\includes\file.inc).
AgAI guess you should use this way:
$file = file_save_data($image, 'public://' . $filename, FILE_EXISTS_RENAME); $node->field_ftest[LANGUAGE_NONE][0]=(array)$file;
xurshid29You should use Form API to create and manage file uploads and their management. You can do something like this instead:
... $form['file'] = array( '#title' => t('File'), '#type' => 'managed_file', '#description' => t('File formats: %format. Maximum: %max_size. Resolution %resolution', array('%format' => 'Jpg, Jpeg, Png, Gif', '%max_size' => '800 Kb', '%resolution' => '80x100')), '#upload_location' => 'public://', ); ...
and submit function:
... $node = node_load(32); $file_d = $form_state['values']['file']; $file = file_load($file_d); $file->status = FILE_STATUS_PERMANENT; file_save($file); file_usage_add($file, 'node', 'TYPE', $file->fid); $file_info = image_get_info($file->uri); $file_arr = array( 'fid' => $file->fid, 'alt' => '', 'title' => '', 'width' => $file_info['width'], 'height' => $file_info['height'], 'uid' => $file->uid, 'filename' => $file->filename, 'uri' => $file->uri, 'filemime' => $file->filemime, 'filesize' => $file->filesize, 'status' => 1, 'timestamp' => $file->timestamp, 'rdf_mapping' => array(), ); $node->field_ftest[LANGUAGE_NONE][0] = $file_arr; node_save($node); ...