gpt4 book ai didi

php - Laravel 文件上传验证

转载 作者:IT王子 更新时间:2023-10-29 01:21:31 24 4
gpt4 key购买 nike

我是 Laravel 的新手。我有一个带有文件上传功能的表单。我如何验证他们的文件?我只允许 Microsoft Word 文件。这是我的验证码。

我只想检查他们是否插入了 ms word 文件,如果没有,则不会被处理。

public function store()
{
// Validate
$rules = array(
'pda' => 'required|unique:forms',
'controlnum' => 'required|unique:forms',
'date' => 'required',
'churchname' => 'required',
'title' => 'required',
'pastorname' => 'required',
'contactnum' => 'required',
'address' => 'required',
'state' => 'required',
'region' => 'required',
'area' => 'required',
'city' => 'required',
'zipcode' => 'required|numeric|max:9999',
'tgjteachertraining' => 'required',
'localcontact' => 'required',
'tgjdatestart' => 'required',
'tgjdateend' => 'required',
'tgjcourse' => 'required|numeric',
'childrengraduated' => 'required|numeric|max:450',
'childrenacceptjesus' => 'required|numeric',
'howmanycomitted' => 'required|numeric',
'recievedbibles' => 'required|numeric',
'descgradevent' => 'required',
'whatwillyoudo' => 'required',
'pastortest' => 'required',
'teachertest' => 'required',
'childrentest' => 'required',
'file' => 'required|max:10000',
);

$validator = Validator::make(Input::all(), $rules);

// process the form
if ($validator->fails()) {
return Redirect::to('forms/create')->withErrors($validator);
} else {
// store
$forms = new Forms;
$forms->pda = Input::get('pda');
$forms->controlnum = Input::get('controlnum');
$forms->date = Input::get('date');
$forms->churchname = ucwords(Input::get('churchname'));
$forms->title = ucwords(Input::get('title'));
$forms->pastorname = ucwords(Input::get('pastorname'));
$forms->address = Input::get('address');
$forms->contactnum = Input::get('contactnum');
$forms->state = Input::get('state2');
$forms->region = Input::get('region2');
$forms->area = Input::get('area2');
$forms->citytown = Input::get('city2');
$forms->zipcode = Input::get('zipcode');
$forms->tgjteachertraining = Input::get('tgjteachertraining');
$forms->localcontact = ucwords(Input::get('localcontact'));
$forms->tgjdatestart = Input::get('tgjdatestart');
$forms->tgjdateend = Input::get('tgjdateend');
$forms->tgjcourse = Input::get('tgjcourse');
$forms->childrengraduated = Input::get('childrengraduated');
$forms->childrenacceptjesus = Input::get('childrenacceptjesus');
$forms->howmanycomitted = Input::get('howmanycomitted');
$forms->recievedbibles = Input::get('recievedbibles');
$forms->descgradevent = Input::get('descgradevent');
$forms->whatwillyoudo = Input::get('whatwillyoudo');
$forms->pastortest = Input::get('pastortest');
$forms->teachertest = Input::get('teachertest');
$forms->childrentest = Input::get('childrentest');
$file = Input::file('file');
$filename = $file->getClientOriginalName();
$destinationPath = 'uploads/'.Input::get('pda');
$uploadSuccess = Input::file('file')->move($destinationPath, $filename);
$forms->docurl = 'uploads/'.Input::get('pda').'/'.$filename;
if( $uploadSuccess ) {
$forms->save();
//Session::flash('message', 'Successfully submitted form!');
return Redirect::to('forms/create');
Session::flash('message', 'Successfully submitted form!');

}
else {
return Response::json('error', 400);
}
}
}

最佳答案

要在 Laravel 中验证文件输入的 mime 类型,您可以使用 mimes 规则。请记住将检测到的 MIME 类型与您提供的文件的实际 MIME 类型相匹配。它可能因不同的服务器而异。

例如,您想在表单中启用添加和 word 文档:

1) 在 config/mimes.php 中添加以下 mime 类型:

    'doc'  => array('application/msword', 'application/vnd.ms-office'),
'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip'),

2) 在您的验证 $rules 中添加以下元素:

    'file' => 'required|max:10000|mimes:doc,docx' //a required, max 10000kb, doc or docx file

关于php - Laravel 文件上传验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23625672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com