9

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.

Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71

6 Answers6

12

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
Erisan Olasheni
  • 2,395
  • 17
  • 20
5

So, as per our discussion in comments and according to Laravel 5.5 docs, your publicly accessible files should be put in directory storage/app/public To make them accessible from the web, you should create a symbolic link from public/storage to storage/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 the asset helper:

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

Hope this will helps you and other users!

Hiren Gohel
  • 4,942
  • 6
  • 29
  • 48
3

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/
Saurabh Mistry
  • 12,833
  • 5
  • 50
  • 71
1

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
ALEXANDER LOZANO
  • 1,874
  • 3
  • 22
  • 31
0

In cPanel or any live server make sure your storage folder and subfolders have the correct permission right. It should be 644, 755 or 777

Samir Rahimy
  • 2,580
  • 1
  • 18
  • 10
0

Since i get used to work on localhost with xampp

I had to add parameter for fix assets url to localhost at .env in main directory of project

ASSET_URL=http://localhost/project_name

I use storage system so more detailed i make url like

http://localhost/project_name/public/storage

So i can use <?= asset('custom_directory/images/brand.png'); ?> to obtain url

I also changed db_host, and memchached_host, also redis_host to localhost to make sure everything goes on with xampp

DB_HOST=localhost
MEMCACHED_HOST=localhost
REDIS_HOST=localhost

As long you use xampp we should respect apache and mysql database and give them localhost isntead of 127.0.0.1 since they treat it diffrently

What is the difference between 127.0.0.1 and localhost

  • 1
    While this code snippet may be the solution, including a detailed explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Shawn Hemelstrand Mar 12 '23 at 11:20