Jake2315's avatar

Why does my image go in a temp file whenever I update my upload?

Whenever I update my image, it goes to some kind of temp file, and I don't know why.

This is the exact output I get after updating my upload:

http://test-ridemibo.c9users.io/storage/avatar//tmp/phpSqpSUe.jpg

My controller:

public function update(Request $request, User $user)
{       
    $user->update($request->all());

    if($request->input('password')) {
        $user->password = Hash::make($request->input('password'));
    }
    
    if($request->hasFile('avatar') && $request->file('avatar')->isValid()) {
        
        $avatar = uniqid();
        
        $request->avatar->storeAs('avatar', $avatar . '.jpg');

        $user->avatar = $avatar;
    }
    
    return redirect('/users/' . $user->id);
}

My blade:

    <div class="col-md-6">
        
        <img class="user_avatar" src="{{ isset($user) && $user->avatar  ? url('/storage/avatar/' . $user->avatar . '.jpg') : url('/storage/avatar/default.jpg') }}">
        
    </div>
0 likes
4 replies
timacdonald's avatar

Couple of things that I could see happening here.

  1. Never being stored.

When you say its a temp file, usually temp files exist when the file is uploaded but never moved. So, I would just throw a quick dd('icon is ok'); in here...

if ($request->hasFile('icon') && $request->file('icon')->isValid()) {
   dd('icon is ok');
   ...

That way you can check that this code is actually going to be executed. If it shows up that its ok, you know that its got to do with the storage mechanism and not the if logic.

Next, it looks like you are trying to store the file in the 'icon' folder, but you need to give a fully-qualified directory, so if you have a folder structure public/icon you need to ensure that it gets the full directory structure. To do that, you can just use the public_path helper function.

So what you would do is...

public function update(Request $request, User $user, Role $role)
{       
    $user->update($request->all());

    if($request->input('password')) {
        $user->password = Hash::make($request->input('password'));
    }
    
    if ($request->hasFile('icon') && $request->file('icon')->isValid()) {
        
        $icon = uniqid();
        
        $request->icon->storeAs(public_path('icon'), $icon . '.jpg');

        $step->icon = $icon;
    }
    
    return redirect('/users/' . $user->id);
}

and blamo - it should be stored in the public_path / icon

Jake2315's avatar

@timacdonald My bad, we are actually talking about avatars, not icons. I copied and pasted the wrong controller, fixed that for you. It's literally same though, just $avatar as variable in stead of $icon. Now, I've noticed that it's not executing my dump and die, meaning there's already something wrong with the if. What could it be?

This is the helpers function I'm using for avatars:

function avatar_url($avatar) {
    return url('avatar/' . $avatar . '.jpg');
}

My config/filesystems.php:

'disks' => [
    
    'local' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
    ],

    'temp_doc' => [
        'driver' => 'local',
        'root' => storage_path('app/pdf/temp_doc'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'visibility' => 'public',
    ],

    's3' => [
        'driver' => 's3',
        'key' => 'your-key',
        'secret' => 'your-secret',
        'region' => 'your-region',
        'bucket' => 'your-bucket',
    ],

],
timacdonald's avatar

I'd do a dd() outside the if on both things, ie hasFile and is valid one at a time to find out which one is failing

jekinney's avatar

Your assuming all avatars are jpgs? If they are not that will fail.

Please or to participate in this conversation.