Laravel验证器和excel文件错误

Exa*_*kun 5 php csv validation excel laravel

我有一个输入字段,可以让人们上传文件.我希望他们可以上传,像doc这样的文件,以及像csv,xlsx这样的文件.

当我尝试.doc没有任何问题,但当我尝试使用excel文件时,验证器失败并说不是好的扩展.

在这里你可以看到我的代码,两行评论是我尝试的另一种解决方案,它也不起作用:(.

欢迎任何帮助.

public function postFile(Request $request)
{ //Règle de validation avec les type de fichiers acceptés

 if(isset($request->file)){
//dd($request);
   $validator=Validator::make($request->all(),[
     'file'=>'required|max:50000|mimes:xlsx,doc,docx,ppt,pptx,ods,odt,odp,application/csv,application/excel,
      application/vnd.ms-excel, application/vnd.msexcel,
      text/csv, text/anytext, text/plain, text/x-c,
      text/comma-separated-values,
      inode/x-empty,
      application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  /*  'extension'  => strtolower($request->file->getClientOriginalExtension()),
     'extension'=>'required|in:doc,csv,xlsx,xls,docx,ppt,odt,ods,odp'*/
   ]);
  if ($validator->fails()) {
     return back()
                ->withErrors($validator);
   }
Run Code Online (Sandbox Code Playgroud)

Exa*_*kun 10

好吧,我的错.我在这个网站上尝试了另一种解决方案,但它确实有用.谢谢你的帮助奥丁.这是我在这个网站上的第一个问题,我会看看我现在是否可以帮助别人.我提出解决方案的代码需要:).

$validator = Validator::make(
  [
      'file'      => $request->file,
      'extension' => strtolower($request->file->getClientOriginalExtension()),
  ],
  [
      'file'          => 'required',
      'extension'      => 'required|in:doc,csv,xlsx,xls,docx,ppt,odt,ods,odp',
  ]
);
Run Code Online (Sandbox Code Playgroud)


Odi*_*der 5

要编写扩展(xlsx,doc,docx请使用“ mime” 。如果使用像application / vnd.ms-excel这样的mime类型,则必须使用验证规则mimetype

更多的mime类型: 更多的mime类型

$validator=Validator::make($request->all(),[
 //use this
    'file'=>'required|max:50000|mimes:xlsx,doc,docx,ppt,pptx,ods,odt,odp'
 //or this
    'file'=>'required|max:50000|mimetypes:application/csv,application/excel,
        application/vnd.ms-excel, application/vnd.msexcel,
        text/csv, text/anytext, text/plain, text/x-c,
        text/comma-separated-values,
        inode/x-empty,
        application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
]
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案对我有用。.xls、.xlsx、.ods 文件的良好验证(通过 mimetypes)。不过,有一个例外。它无法验证使用 libreoffice 创建的 .xls 和 .xlsx 文件。 (2认同)

kej*_*ion 5

下面是我在 Laravel 6 中通过检查文件扩展名的方法。

创建新的验证规则:

php artisan make:rule ExcelRule
Run Code Online (Sandbox Code Playgroud)

这是ExcelRule检查文件扩展名的 :

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Http\UploadedFile;

class ExcelRule implements Rule
{
    private $file;

    public function __construct(UploadedFile $file)
    {
        $this->file = $file;
    }

    public function passes($attribute, $value)
    {
        $extension = strtolower($this->file->getClientOriginalExtension());

        return in_array($extension, ['csv', 'xls', 'xlsx']);
    }

    public function message()
    {
        return 'The excel file must be a file of type: csv, xls, xlsx.';
    }
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我检查csvxlsxlsx在这里。您可以添加所需的任何其他扩展。

在控制器中使用它:

public function uploadExcelFile(Request $request)
{
    $request->validate([
        'excel_file' => ['required', new ExcelRule($request->file('excel_file'))],
    ]);

    $model->update([
        'excel_file' => $request->file('excel_file')->store('excel_files'),
    ]);

    return redirect()->route('my_route_name')->with('Excel file uploaded!');
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*lov 5

这:

'file' => 'required|mimes:xlsx, xls',
Run Code Online (Sandbox Code Playgroud)

可能不起作用,因为您忘记添加此内容:

enctype="multipart/form-data"
Run Code Online (Sandbox Code Playgroud)

在你的 html 表单标签中!