【问题标题】:LARAVEL get errors from isValid in file uploadLARAVEL 在文件上传中从 isValid 获取错误
【发布时间】:2019-01-20 16:27:00
【问题描述】:

我正在使用 Laravel, 到目前为止,所有文件上传都很顺利,所以我从来不用它, 但现在失败了。

我如何了解此代码中文件上传的错误是什么?

if(!request()->file($fileName)->isValid()){
    //some action to learn of the error
}

谢谢

【问题讨论】:

    标签: laravel validation laravel-5 file-upload upload


    【解决方案1】:

    request()->file($fileName) 将返回一个\Illuminate\Http\UploadedFile 的实例。除了isValid(),您还可以调用getError()获取上传错误的识别整数,调用getErrorMessage()获取人为错误字符串。

    $file = request()->file($fileName);
    
    if (!$file->isValid()) {
        $error = $file->getErrorMessage();
        // ...
    }
    

    【讨论】:

    • 太好了,这有帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-04-21
    • 2017-06-19
    • 2017-01-11
    • 2017-07-29
    • 1970-01-01
    • 2016-08-15
    • 2014-02-21
    • 1970-01-01
    相关资源
    最近更新 更多