I have a user profile form. If a user selects a picture, it's uploaded and shows on user profile but If user just updates other details and leaves the picture the same the user_save overrides the user picture and then there is no user picture. My code is like this.

if(!empty($_FILES['files']['name']['profile_image']) && isset($_FILES['files']['name']['profile_image']) && ($_FILES['files']['size']['profile_image'] != 0)) {

        $filepath = 'public://pictures/';
        $file = file_save_upload('profile_image', $validators, $filepath);
        $file->status = 1;
        if(isset($file) && !empty($file) && !is_null($file)) {
            $loaded_user->picture = $file;

  • 1
    Can you post your whole form code, not just this part? Without seeing the rest of the code I would assume you are not setting the previously saved value as the default value for the picture field. – rooby Oct 21 '13 at 10:27
  • Thank you rooby for your comment. This is the section where I add the file. $form['profile']['profile-share']['upload-photo']['upload-pic']['upload'] = array( '#name' => 'files[profile_image]', '#type' => 'file', '#title' => 'profilbild', '#title_display' => 'invisible', '#size' => 30, '#attributes' => array( 'id'=> 'upload', ), ); – rashidkhan Oct 21 '13 at 10:35
  • You're right, I'm not setting the previously set value as a default value. Where can I set it because the #default_value property is not used by file? – rashidkhan Oct 21 '13 at 10:38
  • Can you post the whole code for your form in your original post by editing it, then it will be easier to give a correct answer. - You can change field names etc. if they are sensitive information. – rooby Oct 21 '13 at 10:40
  • The whole form is too big to be posted here but If you could tell me how to set a default value for the file then may be I'll fix it. – rashidkhan Oct 21 '13 at 10:42

I assume you are not setting you default_value for the file field and that you are using a managed_file field.

The default value should be the fid of the file that has been uploaded before.

Something like this:

$form['profile_image'] = array(
  '#title' => t('Profile picture'),
  '#type' => 'managed_file',
  '#description' => t('Upload a photo for your profile.'),
  '#default_value' => $previously_uploaded_file->fid,

In your case I'm not exactly sure where the $previously_uploaded_file->fid will come from, but I assume it will be in your form or form state or else you will have to get it from the database or something.

If you are using a regular file field instead of a managed_file file field I don't believe you can set a default value.

In that case you have to make sure that when you do the user save that you are not overwriting the file with an empty value.

|improve this answer|||||
  • Thank again rooby, it's type is file. I can get the fid from DB. – rashidkhan Oct 21 '13 at 11:01
  • In that case I don't believe that you can use default value. I have added some more info to the answer. – rooby Oct 21 '13 at 12:05
  • +1 for your time. I had already checked the default value for type file. I couldn't solve it, will ask some senior developer in my company to help me. Thank you again. – rashidkhan Oct 21 '13 at 16:29
  • Your use case may be different but I would have thought a user picture would be better as a managed_file? – rooby Oct 21 '13 at 22:03

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.