2017-02-07 53 views
1

我有一個輸入字段,讓人們可以上傳文件。 我希望他們可以上傳,doc文件等文件以及csv,xlsx等文件。Laravel驗證器和excel文件錯誤

當我嘗試使用.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); 
    } 

回答

2

好吧我的錯。 我曾嘗試在這個網站上找到其他解決方案,它的工作。 感謝您的幫助奧丁。 這是我在這個網站上的第一個問題,我會看看我現在是否可以幫助別人。 我在有人需要的解決方案poste代碼:)。

$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', 
    ] 

);

1

當你想要寫的一些推廣(XLSX,DOC,DOCX)。 萬一當使用MIME類型似應用程序/ vnd.ms - Excel中您必須使用驗證規則中使用「默劇」 mimetype

更多MIME類型:more mime-types

$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' 
] 
+0

感謝您的回答,但我已經嘗試了這兩種解決方案;當它嘗試上傳.xlsx或.csv時,驗證器失敗。 – Exarkun

0

首先告訴這不是妥善解決。但你可以試試這個。

我也搜索了這個,並有太多的麻煩驗證excel file和他們的mimes類型不工作的不幸。

if($request->hasFile('file')) 
{ 
    $extension = File::extension($request->file->getClientOriginalName()); 
    if ($extension == "xlsx" || $extension == "xls" || $extension == "csv") { 
     //'Your file is a valid xls or csv file' 
    }else { 
     //'File is a '.$extension.' file.!! Please upload a valid xls/csv file..!!'); 
    } 
} 
在namspace

包括必須use File;

您可以驗證由這樣的任何文件,謝謝。

+0

我找到了一個解決方案,你可以看到最後一個答案,它的工作:) – Exarkun

1

在Laravel中,您可以使用驗證帶有擴展名的文件上傳後掛鉤。從here瞭解更多!

$validator->after(function ($validator) use ($request){ 
    if($this->checkExcelFile($request->file('file')->getClientOriginalExtension()) == false) { 
     //return validator with error by file input name 
     $validator->errors()->add('file', 'The file must be a file of type: csv, xlsx, xls'); 
    } 
}); 

function checkExcelFile($file_ext){ 
    $valid=array(
     'csv','xls','xlsx' // add your extensions here. 
    );   
    return in_array($file_ext,$valid) ? true : false; 
}