You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel无SSH/命令环境下手动创建Storage软链接的方法

Hey there! I’ve dealt with this exact storage link headache on cPanel before—super frustrating when you can’t run artisan commands via SSH. Let’s break down a few solid solutions that should get your storage link working:

1. Fix the AppServiceProvider Implementation

Your initial approach was on the right track, but missing a couple key checks and proper imports. Here’s the corrected version:

First, add this import at the top of app/Providers/AppServiceProvider.php:

use Illuminate\Support\Facades\File;

Then update the boot() method to include existence checks and error handling:

public function boot()
{
    // Only create the link if it doesn't already exist
    if (!File::exists(public_path('storage'))) {
        try {
            File::link(storage_path('app/public'), public_path('storage'));
        } catch (\Exception $e) {
            // Log the error for debugging (check storage/logs/laravel.log)
            \Log::error('Failed to create storage link: ' . $e->getMessage());
        }
    }
}

The main issues with your original code were likely:

  • Not checking if the link already exists (which throws an error if it does)
  • Using a more complex service resolution instead of the cleaner File facade

Many cPanel instances let you create symlinks directly through the file manager—no code needed:

  • Log into cPanel and open the File Manager
  • Navigate to your public_html directory (this is where your Laravel public folder’s files live, or the public folder itself if using a subdomain)
  • Right-click in an empty space and select Create Symbolic Link (look for this option in the top bar dropdown if right-click doesn’t show it)
  • For the Link Path, enter the full server path to your storage/app/public folder. Example: home/yourcpanelusername/laravelproject/storage/app/public
  • For the Link Name, enter storage
  • Click Create—if you get a permission error, double-check that your storage/app/public folder has 755 permissions
3. Use a Temporary PHP Script (One-Time Run)

If the above methods fail, create a quick temporary script to run the link command via your browser:

  • Make a new file named createstorage.php in your public directory
  • Paste this code into it:
<?php
require __DIR__.'/../vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';

// Bootstrap the Laravel application
$app->boot();

use Illuminate\Support\Facades\File;

if (!File::exists(public_path('storage'))) {
    File::link(storage_path('app/public'), public_path('storage'));
    echo "Storage link created successfully! You can delete this script now.";
} else {
    echo "Storage link already exists—no action needed. Delete this script!";
}
  • Visit the script in your browser (e.g., https://yourdomain.com/createstorage.php)
  • Delete the script immediately after it runs—leaving it exposed is a security risk!
Critical cPanel Notes
  • If you’ve moved Laravel’s public folder contents into public_html (standard for cPanel), ensure your paths are correct. public_path('storage') will point to public_html/storage, and storage_path('app/public') points to your Laravel root’s storage/app/public.
  • Permissions matter! Your storage directory and subfolders should be owned by your cPanel user (not root) with 755 permissions for folders and 644 for files.

内容的提问来源于stack exchange,提问作者Kabiru Wahab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:56:48