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:
1. Manually create the symbolic link (if your host allows it)
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(replaceyour-project-folderwith your actual project directory name). - Save the link, then test if your images load properly.
2. Create the symlink via a PHP script
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.
3. Fallback: Copy files directly (if symlinks are blocked entirely)
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
storagefolder 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 rootstoragefolder (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):
Just update your image URLs to use this route instead of the standarduse 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; });/storage/path.
内容的提问来源于stack exchange,提问作者MeliDev

