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

Laravel 5.4:Failed to open stream: Permission denied问题解决咨询

Fixing "Failed to open stream: Permission denied" in Laravel File Uploads

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.

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 $rep variable 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 Report record exists before creating the ReportAttachment.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:45:58