AlViyan Badro Kamali
AlViyan Badro Kamali

Reputation: 89

Laravel storage link in shared hosting

im having problem with setting up storage link on shared hosting. Because of that I can't upload any image to storage folder in shared hosting

Upvotes: 9

Views: 22977

Answers (4)

Yann Boyongo
Yann Boyongo

Reputation: 191

  1. Remove storage folder in public_html/public/storage if you perfomed storage:link in local

  2. then Create a php file for example mysymlink.php in public_html folder

  3. Add below code to mysymlink.php file

<?php
$targetFolder = $_SERVER['DOCUMENT_ROOT'].'/storage/app/public';
$linkFolder = $_SERVER['DOCUMENT_ROOT'].'/public/storage';
symlink($targetFolder,$linkFolder);
echo 'Symlink completed';
  1. Go to web site example.com/mysymlink.php

Upvotes: 13

Adrian
Adrian

Reputation: 320

Just create a route and access it once.

Route::get('generate', function (){
    \Illuminate\Support\Facades\Artisan::call('storage:link');
    echo 'ok';
});

Upvotes: 16

Volod
Volod

Reputation: 1427

Using Laravel in shared hosting environment probably not the best decision. Some many good options are now available for the same price and you just need to put a bit of effort to understand how to work with them, even if you have no prior experience. You can use DigitalOcean, Linode, Vultr or many more and it will save you a ton of time.

In case you for some reason still want to use shared hosting and to actually answer your question you can

  • Investigate to set a right symlink (This possibility nowadays exist almost everywhere) it should be something like ln -s /home/user/laravel/storage/app/public /home/user/public_html/storage. Of course it should by your path which can be different depending of your hosting structure, you should be able to figure out it yourself.
  • Try to implement in some workaround way. For example you can try to put files from storage to storage/app/public folder manually (you even can latter automate it by using some script). But workarounds product new workarounds, better not do it hard and wrong way but instead use simple and right even if it seems hard for you at first glance.

Upvotes: 3

Aditya Thakur
Aditya Thakur

Reputation: 2610

Instead of using Storage facade, you can use custom method to upload file to your public directory

if($request->has('image')){
        $img = $request->file('image');
        $ext = $img->getClientOriginalExtension();
        $name = time().'.'.$ext;
        $path = public_path('\img\uploads');
        $img->move($path, $name);
        User::where('id', $user->id)->update([
            'image' => $name
        ]);
    }

Upvotes: 2

Related Questions