php - Laravel app storage images failed to load and redirect to 404

821

I hosted laravel application on shared hosting server. on server there are 2 folders :

  • 1) laravelapp -> contains (app,config,database,vendor...etc folders)

  • 2) public_html -> contains (css,js,images,storage folders)

I use below code to upload image :

 if($request->hasFile('file')){
     $filenameWithExt=$request->file('file')->getClientOriginalName();
     $filename=pathinfo($filenameWithExt,PATHINFO_FILENAME);
     $extension=$request->file('file')->getClientOriginalExtension();
     $fileNameToStore=str_slug($filename,'_').'_'.time().'.'.$extension;
     request()->file->move(public_path('storage/profile'), $fileNameToStore);
     $image="/storage/profile/".$fileNameToStore;
}

It is successfully upload file tolaravelapp/public/storage/profile folder

I have created symlink tolaravelapp/public/storage withpublic_html/storage

in database : imagepath is saved as/storage/profile/user1.png

in blade template i use :<img src="{{$img}}" />

but imageurl :

www.mydomain.com/storage/profile/user1.png redirect to 404 page and image not displaying .

anyone please help me to solve problem.

739

Answer

Solution:

This worked for me on a shared hosting server:

edit yourconfig/filesystems.php and modify thepublic part like this

'public' => [
        'driver' => 'local',
        'root' => storage_path(),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ]

Notice that the storage_path() does not take any argument. Now removestorage folder from yourpublic directory if already exists, then do:

php artisan storage:link
221

Answer

Solution:

So, as per our discussion in comments and according toLaravel 5.5 docs, your publicly accessible files should be put in directorystorage/app/public To make them accessible from the web, you should create a symbolic link frompublic/storage tostorage/app/public.

ln -s /path/to/laravel/storage/app/public /path/to/laravel/public/storage

Now you can create in your view an URL to the files using theasset helper:

echo asset('storage/file.txt');

Hope this will helps you and other users!

330

Answer

Solution:

1) To store file in laravelapp/storage/app/public/cover/ folder

 use Illuminate\Support\Facades\Storage;
 $path=$request->file('cover_image')->storeAs('public/cover_images',$fileNameToStore);

2) To store file in laravelapp/public/cover/ folder

 $path=request()->cover_image->move(public_path('storage/profile'), $fileNameToStore);

in shared hosting server there are 2 folders :

laravelapp and public_html

As im using 2nd way to upload image , I have solved this issue by creating symlink from laravelapp/public/storage folder to public_html/

ln -s /home/username/laravelapp/public/storage  /home/username/public_html/
830

Answer

Solution:

is posible that the symlink of your storage/public folder was wrong, you can review if this is pointing to the correct folder Use the ls -l command to check whether a given file is a symbolic link, and to find the file or directory that symbolic link point to.

ls -l /usr/bin/python
lrwxrwxrwx 1 root root 9 Apr 16  2020 /usr/bin/python -> python2.7

The first character ā€œlā€, indicates that the file is a symlink. The ā€œ->ā€ symbol shows the file the symlink points to.

The rm command removes given files and directories. To delete a symlink, invoke the rm command followed by the symbolic link name as an argument:

rm symlink_name

create a new symlink to the storage public folder

php artisan storage:link

People are also looking for solutions to the problem: How to sum hours with existing datetime in PHP?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.