0

I have a form that accepts images for profile pics. I know that the form is working, as far as the other fields, but it seems that the file is not even getting POSTed. When I echo out the input, as you can see, I am getting null. This is the first time I am working with file uploads so maybe I am missing something with the way that file inputs POST.

Here is the form open in profile.blade.php

{!! Form::open(['id' => 'editUserProfileForm', 'class' => 'card', 'files' => true]) !!}

Here is the form input in profile.blade.php

<div class="form-group m-b-20">
    <span class="form-group-addon"><i class="zmdi zmdi-account"></i></span>
    <label for="profile_pic">Profile Photo</label>
    <input type="file" id="profile_pic" name="profile_pic" class="form-control">
</div>

Here is the controller method with validation ect. If you see a more succinct way to do what I am doing, I will gladly take suggestions.

public function postEditProfile($id)
    {
        $user = User::find($id);
        $user_information = [
            'name' => Input::get('name'),
            'email' => Input::get('email'),
            'password' => Input::get('password'),
            'password_confirmation' => Input::get('password_confirmation'),
            'profile_pic' => Input::file('profile_pic'),
        ];
        $validator = Validator::make($user_information, [
            'name' => 'required',
            'email' => ($user_information['email'] != $user->email) ? 'required|unique:users' : 'required',
            'password' => (! empty($user_information['password'])) ? 'min:6' : '',
            'password_confirmation' => (! empty($user_information['password'])) ? 'same:password' : '',
            'profile_pic' => (! empty($user_information['profile_pic'])) ? 'mimes:jpeg,bmp,png' : '',
        ]);
        if($validator->fails()) {
            echo json_encode(['st' => 0, 'msg' => $validator->errors()->all()]);
        } else {
            ($user->name != $user_information['name']) ? $user->name = $user_information['name'] : false;
            ($user->email != $user_information['email']) ? $user->email = $user_information['email'] : false;
            (! empty($user_information['password'])) ? $user->password = bcrypt($user_information['password']) : false;
            if(! empty($user_information['profile_pic'])) {
                $profile_pic_destination = 'profile_img';
                $profile_pic_name = rand(11111111, 99999999) . '.' . $user_information['profile_pic']->getClientOriginalExtension();
                $user_information['profile_pic']->move($profile_pic_destination, $profile_pic_name);
                $user->photo = $profile_pic_name;
            }
            if($user->save()) {
                //echo json_encode(['st' => 1, 'msg' => 'Your profile was saved successfully. Boom.']);
                echo json_encode(['st' => 1, 'msg' => $user_information['profile_pic']]);
            } else {
                echo json_encode(['st' => 0, 'msg' => 'The user was not updated.']);
            }
        }
    }

Here is a screenshot of the form POST in the dev console: Google Chrome Dev Console

dericcain
  • 2,182
  • 7
  • 30
  • 52
  • 1
    Just as a side comment, I see you are using rand() to generate the file name, but there is the possibility of overwriting a file still. I would suggest time-stamping the file. Or at least checking that the file doesn't already exist. – Xander Luciano Dec 17 '15 at 19:54
  • @ViperCode Thank you! That is a great idea. – dericcain Dec 17 '15 at 19:55
  • Also, you should really be checking that what the user uploads is *really* an image and not a file with a .jpg extension. It's possible for users to upload scripts this way and cause havoc on your site. This is a fairly simple check to do, see: http://stackoverflow.com/questions/1581136/validate-that-a-file-is-a-picture-in-php – Xander Luciano Dec 17 '15 at 19:57
  • 1
    @ViperCode That's another good suggestion. I believe that I am doing this with Laravel's validation `mime:jpg,bmp,png`. – dericcain Dec 17 '15 at 19:58
  • Try a dd(Input::file('field_name')) if any result shows up, then the file is posted. – Donny van V Dec 17 '15 at 20:22

1 Answers1

0

My apologies, but this seems to be an issue with AJAX and the way that I'm submitting the form. I found a solution here:

How can I upload files asynchronously?

Community
  • 1
  • 1
dericcain
  • 2,182
  • 7
  • 30
  • 52