I need to remove a file field value, and instead, assign a new value to it. The file field contains video and the new video is in the videos directory. This is my code to delete a file field value (which works well)

    $node = node_load(37);
    // Get the language for the file field.
    $lang = field_language('node', $node, 'field_video');
    // load the file
    $file = file_load($node->field_video[$lang][0]['fid']);
    // unset the field for the node
    // delete file from disk and from database
    // Save the node.

But this code which assigns a new value, reports an error. I get this code from How to set value of file field with entity_metadata_wrapper

    $node = node_load(37);
    if ($wrapper = file_stream_wrapper_get_instance_by_uri('public://')) {
      $realpath = $wrapper->realpath();
    $file_path = $realpath."\\video_file\\sarmaye.mp4";
    $file = new stdClass;
    $file->uid = $node->uid;
    $file->filename = pathinfo($file_path, PATHINFO_BASENAME);
    $file->uri = $file_path;
    $file->filemime = file_get_mimetype($file_path);
    $file->display = '1';
    // Make it permanent, otherwise it will get deleted later.
    $file->status = FILE_STATUS_PERMANENT;

    $file = (array) $file;
    $items = array($file);
    $ewrapper = entity_metadata_wrapper('node', $node);

But it reports this error

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'D:\xampp\htdocs\MySiteName\sites\default\files\video_file\sarmaye' for key 'uri': INSERT INTO {file_managed} (uid, filename, uri, filemime, filesize, status, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array ( [:db_insert_placeholder_0] => 1 [:db_insert_placeholder_1] => sarmaye.mp4 [:db_insert_placeholder_2] => D:\xampp\htdocs\MySiteName\sites\default\files\video_file\sarmaye.mp4 [:db_insert_placeholder_3] => video/mp4 [:db_insert_placeholder_4] => 4877369 [:db_insert_placeholder_5] => 1 [:db_insert_placeholder_6] => 1468934410 ) in drupal_write_record() (line 7361 of D:\xampp\htdocs\MySiteName\includes\common.inc).


You have to set the uploaded file object rather then the initial file object. Please check the return value of file_save() function.

The error explained that there is duplicate key uri, which means the code is trying to save the initial file object again.

$ewrapper->field_video->set() only accept (array) $file but not $items.

$file = file_save($file);
$file = (array) $file;
$ewrapper = entity_metadata_wrapper('node', $node);
  • I changed the line $file = file_save($file);. The same error reported. – M a m a D Jul 19 '16 at 14:37
  • Let me try to run this snippet... – Jimmy Ko Jul 19 '16 at 14:42
  • I guess you got error message: EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityMetadataWrapper->set() (line 122 of /var/www/Sites/drupal7.dev/sites/all/modules/contrib/entity/includes/entity.wrapper.inc).. It caused by feeding wrong parameter to set(). I have updated my answer. – Jimmy Ko Jul 19 '16 at 14:49
  • Yep, I got that error. Wait please ... – M a m a D Jul 19 '16 at 15:30
  • I found mistake in code. Please use the most updated one. – Jimmy Ko Jul 19 '16 at 15:34

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.