Shree Krishna Lamichhane

Shree Krishna Lamichhane

Solve Laravel's Storage Shortcut Issue in Free Hosting Plans !

Solve Laravel's Storage Shortcut Issue in Free Hosting Plans !

Subscribe to my newsletter and never miss my upcoming articles

You work hard and make a laravel website. Then you deploy it on Free Hosting services like profreehost, infinityfree. All is working great. When you tried to upload a file, it uploads successfully but while accessing, you get 404 error ?

Lets discuss what's happenning here.

By default, Laravel stores the uploaded content inside root_directory/ storage/ app/ public/ folder. Which is not accessible to the public web. The only folder accessible to public web is /public folder. So by default we used to create a shortcut folder of /storage folder inside of /public folder so that we can access the storage folder from publicly.

That's good but the main problem here is your hosting server can't create/have a shortcut folder. Then how do we solve the problem here?? Laravel's this default storage behaviour is defined in a file at root_directory/ config/ filesystems.php under Filesystem Disks Section .

'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

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

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
        ],
    ],

Amazon s3 is not the problem here. The problem is for local & public . Change the above code section to this.

'disks' => [

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

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

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
        ],

    ],

And boom , now your uploads will be stored in /public/ storage folder and your code should work like a charm for new uploads.

Things to mind with this patch:

Since you have changed the storage folder to public , you should not use strict functions like storage_path() while providing the upload location for upoading the file in respective controller like this :

$path = $request->file('media')->storeAs(storage_path().'media/', $fileNameToStore);

Instead you should use it as :

$path = $request->file('media')->storeAs('media/', $fileNameToStore);

Then there will be no problem at all. I am using this patch for more than a year now and I haven't encountered any problem with it. If you are having any trouble, Let me know . I will be more than happy to help you.

Thanks.

 
Share this