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

Laravel 5.5共享主机环境下如何显示存储文件夹中的图片?

Hey there! Let's work through this storage link problem you're facing on your shared host with Laravel 5.5. Since you've moved all your public folder contents to the root directory, the usual php artisan storage:link command isn't straightforward—plus, shared hosts often restrict shell access anyway. Here are a few solid solutions to get your images showing up:

Most shared hosting panels like cPanel let you create symlinks directly via the File Manager. Here's how to do it:

  • Log into your hosting control panel and open the File Manager.
  • Navigate to your root directory (where you moved all public folder files).
  • Look for the "Create Symbolic Link" option (usually in the top toolbar).
  • For the link name, enter storage (this matches what Laravel expects in the public root).
  • For the target path, enter the relative path to your storage app public folder. Since your project files are in a separate folder, it might look like ../your-project-folder/storage/app/public (replace your-project-folder with your actual project directory name).
  • Save the link, then test if your images load properly.

If you can't create symlinks through the file manager, a quick PHP script can do the job for you:

  • Create a new file in your root directory (name it something like createstorage.php).
  • Paste this code into it, adjusting the paths to match your setup:
    <?php
    // Replace with the correct relative path to your storage app public folder
    $target = '../your-project-folder/storage/app/public';
    // This is the name of the link that will live in your root directory
    $link = 'storage';
    
    if (!file_exists($link)) {
        symlink($target, $link);
        echo "Storage link created successfully! You can delete this script now.";
    } else {
        echo "Storage link already exists!";
    }
    ?>
    
  • Access this script via your browser (e.g., https://yourdomain.com/createstorage.php).
  • Delete the script immediately after seeing the success message—leaving it exposed is a security risk.

If your host doesn't support symlinks at all, you can set up Laravel to automatically copy uploaded files to a public directory in your root:

  • Create a storage folder in your root directory (this is where your views will look for images).
  • Modify your file upload logic to save files to both storage/app/public (for backend storage) and the root storage folder (for public access). You can also use a Laravel observer or post-save hook to handle this auto-copying automatically.
  • Alternatively, create a route to serve files directly from the storage directory (note: this is less efficient than symlinks):
    use Illuminate\Support\Facades\File;
    use Illuminate\Support\Facades\Response;
    
    Route::get('storage/{filename}', function ($filename) {
        $path = storage_path('app/public/' . $filename);
        
        if (!File::exists($path)) {
            abort(404);
        }
    
        $file = File::get($path);
        $type = File::mimeType($path);
    
        $response = Response::make($file, 200);
        $response->header("Content-Type", $type);
        
        return $response;
    });
    
    Just update your image URLs to use this route instead of the standard /storage/ path.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:33:56