Search code examples
phplaravellaravel-storage

Laravel can't upload file in storage folder


I used to store my upload files (images) in my public folder but this time I want to store them in my storage folder and I get this error

Can't write image data to path (C:\laragon\www\mynewsite\storage\images/aboutimage-1522481830.png)

Error comes from this line (where I use intervention/image package)

Image::make($image)->resize(1200, 600)->save($location);

My function:

if ($request->hasFile('image')) {
        $image = $request->file('image');
        $filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
        $location = storage_path('images/' . $filename);
        Image::make($image)->resize(1200, 600)->save($location);

        $oldFilename = $indexabout->image;
        $indexabout->image = $filename;
        Storage::delete($oldFilename);
}

any idea?

UPDATE

My files will upload in root/storage folder instead of root/storage/app/public/images

why is that?

Update 2

I changed my function to code below and it's uploading where it suppose to upload but it does not delete the old image

if ($request->hasFile('image')) {
            $image = $request->file('image');
            $filename = '/aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
            $location = storage_path('app/public/images' . $filename);
            Image::make($image)->resize(1200, 600)->save($location);

            $oldFilename = $indexabout->image;
            $indexabout->image = $filename;
            Storage::delete($oldFilename);
}

Solution

  • SOLVED

    here is final code:

    if ($request->hasFile('image')) {
                $image = $request->file('image');
                $filename = 'aboutimage' . '-' . time() . '.' . $image->getClientOriginalExtension();
                $location = storage_path('app/public/images/' . $filename); // root storage path
                Image::make($image)->resize(1200, 600)->save($location);
    
                Storage::delete('images/' . $indexabout->image); //public storage path
                $indexabout->image = $filename;            
            }
    

    Basically I gave Root/Storage path to store data and Public/Storage path for deleting them in update method.