Search code examples
phpimagefile-uploadproduction-environmentlaravel-10

Laravel Cannot Find Images Stored in Storage Folder


I am currently trying to upload some images and have them displayed with Laravel. All images the that are stored in the public/images work fine. The image cannot be found, however on the linux server where its deployed for production the uploaded image is showing as present in the storage/app/images in the linux terminal through the symbolic link created with php artisan storage:link My code saves the file with the following code

    $this->validate($request, [
            'name' => 'required|max:255',
            'price' => 'required|numeric|gt:0',
            'promotion' => 'numeric|gt:-1',
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
        ]);
        $fileName = time() . '.' . $request->image->extension();
        $request->image->storeAs('public/images', $fileName);

Before saving to an object variable which is then saved in a sqlite database.

I have tried changing the storage folder permissions with chmod 777 storage, editing APP_ENV variables to be in production, updated my APP_URL to the live url, added FILESYSTEM_DRIVER=public. I have repeatedly cleared all the caches and optimizations after each step as well as removing and re-linking the storage folder.

My image link is as such <img src="{{ asset('storage/images/'.$dish->image) }}" />. I have tried changing it to url instead of asset, as well as using Storage::url('storage/images/'.$dish->image) for the src. When I type the precise image path (which I have copied through the linux terminal path command) it displays a 404 page and the real kicker is when I copy image link its an exact copy of the path the image is located https://mysite/storage/images/1695812089.png.

Any help is greatly appreciated.


Solution

  • So its now working, I changed the image tag to this -

    <img src="{{ Storage::disk('public')->url('images/'.$dish->image) }}" />

    As well as adding this rule in the .htaccess file

    RewriteRule ^storage/app/public/(.*)$ public/storage/$1 [L]

    I also did my permissions in a slightly different way than before, I used chmod -R 755 storage and chmod -R 755 public/storage when I didn't include the -R previously.

    I'm pretty sure what fixed it was changing the image tag to call from the public drive where the images were stored, as well as adding the .htaccess rule to let the app know that the storage has been symbolically linked.