laravel中的Excel文件验证不起作用

标签 excel laravel validation

$this->validate($request,
[
   'file'=> 'required|mimes:xlsx'
]);

当我尝试上传 .xlsx 文件时,验证器失败。

最佳答案

您应该尝试以下代码,例如:

$validator = Validator::make(
  [
      'file'      => $request->file,
      'extension' => strtolower($request->file->getClientOriginalExtension()),
  ],
  [
      'file'          => 'required',
      'extension'      => 'required|in:xlsx,xls',
  ]

);

您还可以从 Controller 中检查,例如:
if(Input::hasFile('import_file')){
    $uploadedFileMimeType = Input::file('import_file')->getMimeType();

    $mimes = array('application/excel','application/vnd.ms-excel','application/vnd.msexcel');

    if(in_array($_FILES['import_file']['type'], $mimes)){

        //True
    } else{

        return redirect()->back()->withInput()->withFlashDanger("Please select Only Excel File");

    }
}

关于laravel中的Excel文件验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46090940/

相关文章:

php - Laravel 生成器给出 : Cannot redeclare generatorFunction()

laravel - 使用 Laravel 模型更新表

php - Laravel 5.4 内部错误 : Failed to retrieve the default value

c# - 验证 .vhd 文件实际上是 vhd

javascript - Mongoose - 所需的验证器未按功能运行

vba - 获取 Collection 对象上项目的键

excel - 加载 DLL 错误,丢失 : Microsoft Word 16. 0 对象库

excel - 统计按一定分隔符分割的数据条数

python - 将 MultiIndex 列组合到 Pandas 数据框中的单个索引

c# - ASP.NET MVC。是否可以使用 MVC Foolproof Validation 进行数组验证?