Search code examples
laravellaravel-5filesystemshostingweb-deployment

laravel, How to save images to public_html directory


I have deployed my page on host. And I have problem, when I posting images.

All images are stored into app/public/images

but I need store them into public_html/public/images

filesystems.php file:

'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => public_path(),
        ],

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

what changes need to be done here?


Solution

  • It is not good idea to store them in public_html/images seems that you are using CPanel, so you should change your root directory to public_html/public in the CPanel, sometimes you should ask your provider to do this.

    then you can follow Laravel instruction to store on app/public after that it's necessary to create a symlink on the public directory as in Laravel document is mentioned.

    Trust me, there is so many ways to do that, but this way is both clear and secure.

    finally, to store data in public directory follow this code:

    $path = 'images';
    $newFilename = Storage::disk('public')->store($path);
    

    So, based on the symlink description which is in this link, your first public directory will be storage, so when you want to use this link in your, use following code:

    Storage::disk('public')->url($newFilename);
    

    and the $newFilename will be consist of \images` in itself.

    To sum up, The file will be stored at \public_html\public\storage\images, and you will have the file partially path in return of store method, and by calling url method you will have the full url by laravel iteself.