mooseh's avatar

Why you cache like this?

Hi everyone

So i've just made a simple image upload for my website but i have a problem

laravel is caching the original image.

here's my code so far as you can see if the file exists it deletes it and adds the new one uploaded but the old one displays? I have tried using cache::flush(); but that did nothing, I checked the image on the mounted drive to my website and the new one IS there.

how can i get it to reset the cache if it its replacing the file?

   public function upload(Request $request){

    $id = Auth::user()->id;

    $path = public_path().'/img/members/'.$id;

    if (!file_exists($path)) {
       File::makeDirectory($path, $mode = 0777, true, true); 
    }

    if (file_exists($path.'/profile_pic.jpg')) {
       File::delete($path.'/profile_pic.jpg');
    }


    if($request->hasFile('profile_pic')) {
        $file = Input::file('profile_pic');

        $file->move($path.'/', 'profile_pic.jpg');

    } else {
        return Redirect()->back()->with('error','this is not a valid type');
    }
        return Redirect()->back();

    }

}
0 likes
6 replies
Erwin's avatar
Erwin
Best Answer
Level 3

Are u using the same name for the new image? Maybe it's your browser cache. Try CTRL F5.

mooseh's avatar

yeh I thought it might be that but i tried using firefox after i made the change and its still the old image :(

Erwin's avatar

Are you sure the old one is deleted? Don't think this has anything to do with Laravel.

Snapey's avatar

My bet is browser cache. Use the network tools in your browser to check if the image is actually downloaded and not 'from cache' and that the path is as you expect.

by the way, you are very trusting with your uploads. You are assuming it must be a jpg and not checking it for size or type?

mooseh's avatar

you were all right, i dont understand how the cache was still there in firefox even though it hadnt been used on the site before, im guessing im going to have to store the image name in the DB and load it that way with a different name each time :(

Snapey's avatar

you can add a fake parameter to the end of the image name. So called cache busting.

In your image link, tag something on the end.

<img src="/img/members/1234/profile_pic.jpg?23:59">

the parameter on the end does not affect loading of the image, but forces it to be fetched fresh if the parameter changes. in this case i used the hours and minutes from the current time. each time you render the page the time will be different and the parameter will change.

Please or to participate in this conversation.