omrakhurs's avatar

Cannot upload image with Intervention image

I am trying to upload an image using the tutorial here. I have followed the instructions exactly as in the video, but once I click on the submit button, it brings me back to the profile page, and the image is unchanged, nothing has changed in the database or uploaded to the folder designated. I am using Laravel 5.2.

Here is my routes.php:

Route::get('profile', 'UserController@showProfile');
Route::post('profile', 'UserController@updateAvatar');

Here is the blade file:

<div class="row">
   <div class="col-sm-4">
       <div class="text-align-center">
           <img class="img-circle" src="/uploads/avatars/{{ Auth::user()->avatar }}" alt="64x64" style="height: 112px; border-radius:50%;">
       </div>
       <br>
       <br>
       <div class="text-align-center">
          <form enctype="multipart/form-data" action="{{ url('/profile') }}" method="POST">
          <h5>Update Profile Image</h5>
          <input type="file" name="avatar">
          <input type="hidden" name="_token" value="{{ csrf_token() }}"> <br>          
          <input type="submit" class="pull-left btn btn-sm btn-primary">
          </form>
       </div>
</div>

Here is the controller:

 public function showProfile(){

        return view('profile', array('user' => Auth::user()));
    }

    public function updateAvatar(Request $request){
        //Handle the user upload of avatar
        if($request->hasFile('avatar')){
            $avatar = $request->file('avatar');
            $filename = time() . '.' . $avatar->getClientOriginalExtension();
            Image::make($avatar)->resize(200, 200)->save( public_path('/uploads/avatars/' . $filename ) );
            $user = Auth::user();
            $user->avatar = $filename;

            $user->save();
        }

        return view('profile', array('user' => Auth::user()));
    }

I have added a field avatar to the users table.

0 likes
17 replies
joedawson's avatar

Is the avatar fillable on your User model?

Also you have broken syntax in the code you provided?

File:: // what's this supposed to be?
omrakhurs's avatar

@JoeDawson , my bad. That was not supposed to be there. I have updated the question. avatar isn't fillable at the moment.

omrakhurs's avatar

@JoeDawson , made it fillable in the User model. Still not working. I'm still unsure about some of the parts in the lines below due to my limited knowledge of Laravel. I don't know if some of those functions like public_path and Image::make() are working right

    $filename = time() . '.' . $avatar->getClientOriginalExtension();
        Image::make($avatar)->resize(200, 200)->save( public_path('/uploads/avatars/' . $filename ) );
omrakhurs's avatar

@JoeDawson , I did a var_dump on the request object, and the result was NULL. Does that mean there's something wrong with the routes or blade?

omrakhurs's avatar

@tomi , now it gives a huge feedback, but before I could copy it, the page crashes. I could see a lot of null arrays though

joedawson's avatar

In your controller, do a dd() on the request.

dd($request->all());

Do you see any data?

omrakhurs's avatar

@JoeDawson :

I did that and I think the problem was as follows:

The UserController is in fact a resource controller I made with artisan. Now it already has its own CRUD methods, plus in the routes file I also have a Route::resource('users','UserController');

Now inside of this controller, I had created the updateAvatar function and given it a route of

Route::post('profile', 'UserController@updateAvatar');

What I did then was do a dd on the request while it was being sent to updateAvatar. Got no response, just a blank page. Then I put the dd in the UserController@store method, and in the form set the

``route`=>`users.store`` 

Now it successfully shows the following:

array:2 [▼
    "_token" => "of2rLg73Yzdj8pbM8nmqlUPSxSbHdSrRoPzTBQoI"
    "avatar" => "089253d.jpg"
]
omrakhurs's avatar

However, when I do a

if($request->hasFile('avatar')){
    die('there is a file');
}
else
    die('theres no file');

it says that theres no file.

omrakhurs's avatar

It's just a file selection button, and a submit button.

omrakhurs's avatar

@JoeDawson , sorry about that mate, thought I'd put the new form code above as well:

{{Form::open(['method'=>'POST','route' => 'users.store', 'files' => true, 'role'=> 'form']) }}
    {{Form::label('file', 'Update image') }}
    {{Form::file('avatar',null)}}
    {{Form::submit('Update',['class' => 'pull-left btn btn-sm btn-primary']) }}
    {{Form::close() }}
joedawson's avatar

@omrakhurs no problem man - what happens when you do a dd() of the file itself?

dd($request->file('avatar'));

You should have an UploadedFile object.

joedawson's avatar

Weird...

Try these routes.

Route::post('users/avatar', ['as' => 'users.updateAvatar', 'uses' => 'UserController@updateAvatar']);
Route::resource('users','UserController');

Update your form opening to use the route users.updateAvatar. You can also remove the method part as it will use POST method as a default.

{{Form::open(['route' => 'users.updateAvatar', 'files' => true, 'role'=> 'form']) }}

Now try dd() your avatar file.

dd($request->file('avatar'));

Do you still get the same?

omrakhurs's avatar

Okay, accidental solution found. I deleted the entire blade file. Made a new form in the new blade file and stored it with a different name. Now when I dd, it says that there is a file. I also did a view:clear on artisan.

Please or to participate in this conversation.