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

Laravel 9无法访问storage目录公开文件问题排查

PHP 8.2升级后Laravel存储文件404问题排查

问题背景

近期将API从PHP 7.4升级至PHP 8.2,此前在Laravel 8环境下运行正常。我有一个Form模型用于存储PDF文件列表:
Form模型存储的PDF文件列表

这些PDF原本可通过公开存储链接访问,例如:https://schedule-api.local/storage/forms/51a59440e6d874b7ed2c91882efaf80c.pdf,但升级后访问该链接出现404错误:
文件访问404错误页面

检查laravel.log未发现错误信息,查看/var/log/apache2/schedule-api-error.log得到如下内容:
Apache错误日志内容

已做排查操作

  • 已执行php artisan storage:link命令
  • 在Ubuntu系统中直接访问/var/www/html/schedule-api/storage/app/public/forms目录下的PDF文件,可正常打开

相关代码片段

Form模型的file属性修改器

public function setFileAttribute($value): void
{
    $file = $value;

    if ($file instanceof UploadedFile && $file->isValid()) {
       $oldFile = $this->attributes['file'] ?? null;

       // 移除旧文件
       if (! empty($oldFile)) {
          $value = $oldFile;
          $path = "forms/{$oldFile}";

          if (Storage::disk('public')->exists($path)) {
             Storage::disk('public')->delete($path);
          }
        } else {
           $extension = $file->guessExtension();
           $value = self::randomFileName().'.'.$extension;
        }

        $file->storeAs('forms', $value, ['disk' => 'public']);
   }

   $this->attributes['file'] = $value;
}

项目根目录的.htaccess文件

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

疑问

请问我哪里操作有误?


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 18:23:31