Laravel 8 文件上传验证失败,任何规则

cam*_*lle 5 php validation upload file laravel

我想验证文件上传,但实际上任何验证规则,我都会收到“(输入名称)上传失败”。我在几个地方看到过这个问题,但没有一个解决方案对我有用。

我在 ubuntu 上使用 Laravel 8.0、php 8.0.2 和 nginx/1.18.0。

控制器:

public function uploadMedia (Request $request)
{
    $request->validate([
        'file' => 'required',
        'alt' => 'required',
    ]);
    dd('valid');
}
Run Code Online (Sandbox Code Playgroud)

刀片文件:

@if($errors->any())
    @foreach ($errors->all() as $error)
        <p class="alert error">{{ $error }}</p>
    @endforeach
@endif

<form method="POST" action="/media" enctype="multipart/form-data">
    @csrf
    <input type="file" name="file" />
    <input type="text" name="alt" placeholder="Write a short description of the image under 160 characters." />
    <button type="submit">Upload</button>
</form>
Run Code Online (Sandbox Code Playgroud)

如果我摆脱了“文件”的验证规则,它会起作用,我会进入 dd。

$request->validate([
    'file' => '', // works if I do this
    'alt' => 'required',
]);
Run Code Online (Sandbox Code Playgroud)

我见过其他人有这个问题,我试过:

  • 为文件放置另一个规则(最大:10000,图像)而不是“必需” - 仍然得到相同的错误。
  • 将值 php.ini 更改为post_max_size = 201Mupload_max_filesize = 200M(这首先应该不是问题,因为我尝试上传的图像是 136kb jpg)。验证与phpinfo();
  • 更改这些值后,重新加载 nginx 和 php-fpm
  • 重新启动虚拟机
  • 检查所有双引号和单引号都是正确的字符
  • 尝试上传其他文件类型,如 png 或 txt,同样的错误。
  • 将“图像”作为第一个适用于此处的验证规则
  • 删除规则数组末尾的多余逗号
  • 将文件输入的名称更改为“file”以外的名称
  • 使用不同的浏览器(Firefox 和 Chrome)
  • 禁用我的所有浏览器扩展
  • 像这样编写验证(仍然得到相同的错误):
$validator = Validator::make($request->all(), [
    'file' => 'required',
    'alt' => 'required'
]); 
if ($validator->fails()) {
    return redirect('/media')->withErrors($validator);
}
Run Code Online (Sandbox Code Playgroud)

如果我在验证之前 dd($request) :

Illuminate\Http\Request {#44 ?
  #json: null
  #convertedFiles: null
  #userResolver: Closure($guard = null) {#255 ?}
  #routeResolver: Closure() {#264 ?}
  +attributes: Symfony\Component\HttpFoundation\ParameterBag {#46 ?}
  +request: Symfony\Component\HttpFoundation\ParameterBag {#45 ?
    #parameters: array:2 [?
      "_token" => "RrjAA2YvnSd3EYqg8vAwoWT4y6VenJzGjb5S72SU"
      "alt" => "dsfghdf"
    ]
  }
  +query: Symfony\Component\HttpFoundation\InputBag {#52 ?}
  +server: Symfony\Component\HttpFoundation\ServerBag {#49 ?}
  +files: Symfony\Component\HttpFoundation\FileBag {#48 ?
    #parameters: array:1 [?
      "file" => Symfony\Component\HttpFoundation\File\UploadedFile {#33 ?
        -test: false
        -originalName: "Screen Shot 2021-03-08 at 9.33.19 AM.png"
        -mimeType: "application/octet-stream"
        -error: 6
        path: ""
        filename: ""
        basename: ""
        pathname: ""
        extension: ""
        realPath: "/var/www/[my domain]/public"
        aTime: 1970-01-01 00:00:00
        mTime: 1970-01-01 00:00:00
        cTime: 1970-01-01 00:00:00
        inode: false
        size: false
        perms: 00
        owner: false
        group: false
        type: false
        writable: false
        readable: false
        executable: false
        file: false
        dir: false
        link: false
      }
    ]
  }
  +cookies: Symfony\Component\HttpFoundation\InputBag {#47 ?}
  +headers: Symfony\Component\HttpFoundation\HeaderBag {#50 ?}
  #content: null
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: "/media"
  #requestUri: "/media"
  #baseUrl: ""
  #basePath: null
  #method: "POST"
  #format: null
  #session: Illuminate\Session\Store {#296 ?}
  #locale: null
  #defaultLocale: "en"
  -preferredFormat: null
  -isHostValid: true
  -isForwardedValid: true
  -isSafeContentPreferred: null
  basePath: ""
  format: "html"
}
Run Code Online (Sandbox Code Playgroud)

mik*_*n32 1

误差值为 6 表示UPLOAD_ERR_NO_TMP_DIR. php -i通过从命令行(或phpinfo();从网页)运行并检查upload_tmp_dir密钥,确保您的系统具有正确配置的上传临时目录。在典型的 Linux 系统上,这将类似于/tmp. 如果需要,您可以设置该值php.ini。确保列出的文件夹的权限正确,以便允许 Web 服务器进程写入该文件夹。

不要尝试使用可公开访问的文件夹之一作为上传目录(例如直接保存到或类似的目录)。您的应用程序代码应按照文档中的说明storage/app/public将文件移动到存储中;像这样的东西:

public function uploadMedia(Request $request)
{
    $request->validate([
        'file' => ['required', 'file', 'image'],
        'alt' => ['required', 'string'],
    ]);
    $path = $request->file->store('images');
    return redirect()->route('whatever')->with('success', "File saved to $path");
}
Run Code Online (Sandbox Code Playgroud)