Laravel File Upload Validation
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Laravel File Upload Validation: Enforcing Microsoft Word Files with Custom Code
In your Laravel application, you have a form that allows users to upload files, and you need to validate their file type as Microsoft Office Word documents (DOCX or DOC). By following the code example below, you can ensure that only allowed Microsoft Word file types are processed. Let's walk through the process step-by-step:
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', // Max upload file size is 10MB
);
$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');
// Add custom validation for file type.
if (! $file->isValid() || ! $file->getClientOriginalExtension() === 'docx' && ! $file->getClientOriginalExtension() === 'doc') {
return Redirect::back()->withErrors(['File is not a Microsoft Word Document!']);
}
// Check if the uploaded file is valid.
else if ($file->isValid()) {
$filename = $file->getClientOriginalName();
$destinationPath = 'uploads/'.Input::get('pda');
$uploadSuccess = Input::file('file')->move($destinationPath, $filename);
if ($uploadSuccess) {
// Save the file details.
$forms->docurl = 'uploads/'.Input::get('pda').'/'.$filename;
$forms->save();
Session::flash('message', 'Successfully submitted form!');
} else {
return Response::json('error', 400); // Return error if file upload fails.
}
}
}
}
In this example, we have added a custom validation rule to check the file type before processing it. The code makes sure that only Microsoft Word documents are accepted for upload. If any other file type is provided, the user will get an error message indicating that they need to choose a valid Microsoft Word document. To ensure compatibility with Laravel 5.7 or above, use the `isValid()` method on the file object instead of `getClientOriginalExtension()`.