Can't do php artisan storage:link, The storage folder cannot be connected to the public folder. How to fix this issue?

Updated: Feb 04, 2025

Can't do php artisan storage:link, The storage folder cannot be connected to the public folder. How to fix this issue?

The error message "Can't do php artisan storage:link, The storage folder cannot be connected to the public folder" typically occurs when the Laravel application is unable to create a symbolic link between the storage and public directories. This link is necessary for serving files from the storage directory through a public URL. Here are some steps to help you fix this issue:

  1. Check file permissions: Ensure that the storage and public directories have the correct file permissions. The storage directory should be writable by the web server user, while the public directory should be readable by everyone. You can use the following commands to check and set the correct permissions:

    sudo chmod -R 755 storage bootstrap/cache
    sudo chmod -R 775 public
    

    Replace "sudo" with "sudo su" or your preferred method to run commands as the root user if you're using a shared hosting environment.

  2. Create the symbolic link manually: If the file permissions are correct, you can create the symbolic link manually using the following command:

    php artisan storage:link
    

    If you encounter a permission denied error, try running the command with the --force option:

    php artisan storage:link --force
    

    This command will create the symbolic link even if the storage directory isn't writable by the web server user. However, it's not recommended to use this option in a production environment.

  3. Use a different disk configuration: Laravel supports multiple disk configurations. You can configure a different disk for storing files and serving them through a public URL. Here's how to do it:

    a. Edit the .env file and set the following variables:

    FILESYSTEM_DRIVER=local
    STORAGE_DRIVER=public
    

    b. Create a new directory for public files in the public directory:

    mkdir public/storage
    chmod -R 775 public/storage
    

    c. Update the filesystems.php configuration file to use the new public disk:

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

    d. Update your Laravel code to use the new public disk when serving files:

    use Illuminate\Support\Facades\Storage;
    
    // ...
    
    return Storage::disk('public')->get('path/to/file.jpg');
    

    This configuration uses the local filesystem for storing files in the storage directory and the public filesystem for serving files from the public/storage directory. This setup is more secure and allows you to serve files directly from the public directory without creating a symbolic link.

  4. Use a file server or a CDN: If you're dealing with large files or a high volume of traffic, it's recommended to use a file server or a Content Delivery Network (CDN) to serve your files. This setup separates the file serving from your Laravel application and eliminates the need for symbolic links or public file directories. You can use popular file servers like Amazon S3, Google Cloud Storage, or Microsoft Azure Blob Storage, or CDNs like Cloudflare or Amazon CloudFront.

    To use a file server or a CDN, you'll need to update your Laravel configuration and code to use the new file storage location. Consult the documentation of your chosen file server or CDN for detailed instructions.