php 无法访问存储文件夹中的公用文件:拉腊维尔9

6pp0gazn  于 2023-04-19  发布在  PHP
关注(0)|答案(1)|浏览(134)

我最近升级了我的API到PHP 8.2(从7.4),它在Laravel 8中工作得很好。我有一个Form模型,其中包含PDF列表:

这些PDF可以通过公共存储访问,如下所示:
https://schedule-api.local/storage/forms/51a59440e6d874b7ed2c91882efaf80c.pdf但现在当我尝试访问该文件时,我得到了以下内容:

我检查了laravel.log-没有错误,我检查了/var/log/apache2/schedule-api-error.log,我得到了这个:

其他信息:

  • 我已经执行了php artisan storage:link
  • 当我使用Ubuntu从/var/www/html/schedule-api/storage/app/public/forms目录访问这些PDF时-它工作得很好

下面是file属性的mutator:

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

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

       // Remove old file
       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>

我做错了什么?

kxeu7u2r

kxeu7u2r1#

尝试创建一个控制器方法,并根据文件名返回文件。类似于以下内容:

public function getMyFile(string $filename){
  $form = Form::where('file',$filename)->first();
  abort_if(!$form, 404);
  $path = storage_path('public/forms/' . $filename);
  return Response::make(file_get_contents($path), 200, [
    'Content-Type' => 'application/pdf',
    'Content-Disposition' => 'inline; filename="'.$filename.'"'
  ]);

}

相关问题