Laravel存储文件夹返回403 Forbidden,文件上传失败求助
Hey there, let's work through your file upload issue step by step. I've reviewed your controller code and filesystem config, and here are the most likely fixes to get your logos uploading properly:
1. Ensure Your Form Has the Correct Enctype
First off, double-check your upload form includes enctype="multipart/form-data" — without this, the file won't actually be sent to the server at all, even if the path gets saved to the database. Your form should look something like this:
<form method="POST" action="/leagues" enctype="multipart/form-data"> @csrf <input type="text" name="name" placeholder="League Name"> <input type="file" name="logo"> <button type="submit">Add League</button> </form>
2. Add Validation & Error Handling to Your Controller
Right now, your code doesn't validate the uploaded file or catch any upload errors. This makes it impossible to know why the upload is failing. Let's update the controller to include validation and exception handling:
public function addLeague(Request $request) { // Validate input first $validated = $request->validate([ 'name' => 'required|string|max:255', 'logo' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048', // Adjust rules as needed ]); $newLeague = new League(); $newLeague->name = $validated['name']; if ($request->hasFile('logo') && $request->file('logo')->isValid()) { try { // Store the file and save the path $newLeague->logo = $request->file('logo')->store('images'); } catch (\Exception $e) { // Catch and display upload errors for debugging return back()->withErrors(['logo' => 'Logo upload failed: ' . $e->getMessage()]); } } $newLeague->save(); return redirect('/leagues'); }
This will help you pinpoint exactly what's going wrong (e.g., file too large, invalid file type, permission issues).
3. Verify Storage Folder Permissions & Existence
Even if you've adjusted permissions before, make sure the storage/app/public/images folder exists and is writable by your web server user:
- First, create the folder if it doesn't exist:
mkdir -p storage/app/public/images - Set the correct ownership and permissions (use
www-datafor Apache/Nginx on Linux):chown -R www-data:www-data storage/app/public/images chmod -R 755 storage/app/public/images
For local development (like Valet or Homestead), you might use your user account instead:
chown -R $USER:$USER storage/app/public/images
4. Confirm Filesystem Configuration
Check your .env file to ensure the default filesystem driver is set to local (since your config uses storage/app/public as the root for local):
FILESYSTEM_DRIVER=local
If this was accidentally set to s3 or another cloud driver without proper credentials, the file would fail to upload while the path still gets saved to the database.
5. Recheck the Storage Link
Even though you said you created the storage link, it's worth verifying it's correctly pointing to storage/app/public. Run this command to check:
ls -la public/storage
If the link is broken or missing, recreate it with:
php artisan storage:link
On shared hosting where you can't run Artisan commands, you'll need to manually create a symbolic link via your host's file manager or SSH.
内容的提问来源于stack exchange,提问作者Hamza Hamdani

