Laravel 5.4:Failed to open stream: Permission denied问题解决咨询
Hey there! That permission error when trying to upload files to your public/reportAttachment directory is a super common hiccup in Laravel—let's work through fixing it step by step:
1. Verify the target directory exists and set correct permissions
First off, Laravel's storeAs('public/...') doesn't save directly to your root public/ folder. It uses the storage/app/public/ directory, which is linked to the public folder via a symlink. So first, make sure your reportAttachment subfolder exists here:
mkdir -p storage/app/public/reportAttachment
Next, you need to ensure the web server user (usually www-data on Debian/Ubuntu, apache on CentOS/RHEL) has read/write access to this directory. Run these commands:
# Grant read/write/execute permissions to the directory chmod -R 755 storage/app/public/reportAttachment # Assign ownership of the directory to your web server user chown -R www-data:www-data storage/app/public/reportAttachment
Note: If you don't know your web server user, run ps aux | grep apache or ps aux | grep nginx to find it, then replace www-data with that user.
2. Ensure the storage symlink is set up
Laravel relies on a symlink from public/storage to storage/app/public so external users can access uploaded files. If this link doesn't exist, create it with:
php artisan storage:link
If you're working on a shared host and can't run artisan commands, you can create the symlink manually via FTP or your host's file manager.
3. Tweak your code for robustness (and debugging)
Looking at your code, there are a couple small tweaks to make it more reliable, plus a debug check to confirm permissions are working:
- You're reusing the
$repvariable in your loop (foreach($rep as $rep)), which will overwrite your original array—rename the loop variable to avoid this. - Add a check to make sure the target directory is writable before trying to save the file.
- Verify the
Reportrecord exists before creating theReportAttachment.
Here's the updated code:
if(!empty($rep)) { foreach($rep as $file) { // Rename loop variable to avoid overwriting $filename = $file->getClientOriginalName(); $fileSize = $file->getClientSize(); // Debug check: Verify directory is writable $targetDir = storage_path('app/public/reportAttachment'); if(!is_writable($targetDir)) { dd("Error: Directory $targetDir is not writable. Check permissions!"); } $file->storeAs('public/reportAttachment', $filename); // Ensure we found a matching Report record $report = Report::where('reportId', $request->reportId)->first(); if($report) { $reportA = new ReportAttachment; $reportA->reportAttachmentFile = $filename; $reportA->report_id = $report->id; $reportA->save(); } else { dd("Error: No Report found with reportId {$request->reportId}"); } } }
4. Check SELinux (if you're on a RHEL/CentOS server)
If you're using a server with SELinux enabled (common on CentOS/RHEL), even correct file permissions might not be enough. You'll need to set the proper SELinux context for the directory:
chcon -R -t httpd_sys_rw_content_t storage/app/public/reportAttachment
That should cover all the common causes of this permission error. Give these steps a try, and you should be uploading files smoothly in no time!
内容的提问来源于stack exchange,提问作者Nida Akram

