0

I am running into an issue where the uploaded image gets unset after few hours while uploading a new user picture.

Also the previous picture does not gets deleted and throws an error "File cannot be deleted because it's not a valid uri".

Here is my save function that updates the user picture programmatically.

 $userimage = $form_state['values']['userimage']; //fid of the file in custom form
 $user_image_file = file_load($userimage);
 $wrapper = entity_metadata_wrapper('user', $user);

 // get image information
 $image_path = file_create_url($user_image_file->uri);
 $image_info = image_get_info($image_path);

 // create file object
 $file = new StdClass();
 $file->uid = $uid;
 $file->uri = $image_path;
 $file->filemime = $image_info['mime_type'];
 $file->status = 1; // Yes! Set status to 0 in order to save temporary file.
 $file->filesize = $image_info['file_size'];
 $file->filename = drupal_basename($image_path);
 $file->status   = FILE_STATUS_PERMANENT;
 $file = file_save($file); 
 // save file to user entity
 $edit['picture'] = $file;
 user_save($user, $edit);

The image just vanishes and gives a broken url after few hours. Any directions?

Thanks

  • 2
    Why are you duplicating the file entry? Why not edit the one thats already been uploaded? Then the issues (probably caused by duplicating path/file name for 2 diffferent file entities) will disappear. – Clive Dec 13 '16 at 9:13
  • or unset and then add. – No Sssweat Dec 13 '16 at 9:21

Your Answer

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

Browse other questions tagged or ask your own question.