Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel app storage images failed to load and redirect to 404

Tags:

php

image

laravel

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 to laravelapp/public/storage/profile folder

I have created symlink to laravelapp/public/storage with public_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.

like image 577
Saurabh Mistry Avatar asked Mar 26 '18 10:03

Saurabh Mistry


1 Answers

This worked for me on a shared hosting server:

edit your config/filesystems.php and modify the public 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 remove storage folder from your public directory if already exists, then do:

php artisan storage:link
like image 159
Erisan Olasheni Avatar answered Sep 22 '22 09:09

Erisan Olasheni