0

i tried to update file cck field of a content type as below from another node as below.

<form action="" method="post" id="tfrm" enctype="multipart/form-data">
    <input id="tf" name="tf" type="file" />
    <input id="btn" type="submit" value="Submit" name="btn" />
</form>
<?php 
if($_POST['btn']){  
$node=node_load(32);

$filename = basename($_FILES['tf']['name']);
$image = file_get_contents($_FILES['tf']['tmp_name']);
$file = file_save_data($image, 'public://' . $filename, FILE_EXISTS_RENAME);
$filesinfo = (object) array(
          'uid' => 1,
          'uri' => $file->uri,
          'filemime' => file_get_mimetype($file->uri),
          'status' => 1,
 ); 
$node->field_ftest[LANGUAGE_NONE][0]=(array)$filesinfo;
print_r($filesinfo);
  node_save($node);

}
?>

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).
1

You 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);
...
2
  • yes thats the clean way but i'm restricted to add custom module
    – ArK
    Feb 8 '14 at 9:04
  • @Paniyar you don't need to create a custom module.. You can just create a form even in template.php and embed it anywhere with drupal_get_form() function. Form generation callback is just a PHP function..
    – xurshid29
    Feb 8 '14 at 9:07
1

I guess you should use this way:

$file = file_save_data($image, 'public://' . $filename, FILE_EXISTS_RENAME);
$node->field_ftest[LANGUAGE_NONE][0]=(array)$file;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.