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

Laravel 8 file upload validation fails with any rule

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

我在 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');
}

Blade 文件:

@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>

如果我摆脱了 'file' 的验证规则它起作用了,我得到了 dd。

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

我看到其他人遇到过这个问题,我已经尝试过了:

$validator = Validator::make($request->all(), [
    'file' => 'required',
    'alt' => 'required'
]); 
if ($validator->fails()) {
    return redirect('/media')->withErrors($validator);
}

如果我在验证之前 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"
}

错误值 6 表示 UPLOAD_ERR_NO_TMP_DIR. Ensure that your system has a properly configured upload temp directory by running php -i from command line (or phpinfo(); from a web page) and checking for the upload_tmp_dir key。在典型的 Linux 系统上,这类似于 /tmp。如果需要,您可以在 php.ini 中设置值。确保所列文件夹的权限正确,以便允许 Web 服务器进程写入。

不要尝试将您的一个可公开访问的文件夹用作上传目录(例如,直接保存到 storage/app/public 或类似目录。)您的应用程序代码应该将文件移动到存储空间 as described in the documentation;像这样:

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");
}