How to validate the file name in Laravel 5.4


I have a form with three input fields. I want to validate the input value before processing them. Where I want to validate the file name before processing it. I use regular expression and alpha_dash. But I got an error for a valid file name. I want my file name only to contain small letter, numbers, underscore and dashes. How can I check the validation of the file name for my file?


<form action="create" method="POST" enctype="multipart/form-data"> {{csrf_field()}} <table cellpadding="2" width="20%" align="center" cellspacing="2"> <tr> <td colspan=2> <center><font size=4><b>Add the iteams please</b></font></center> </td> </tr> <tr> <td>Heading</td> <td><input type="text" name="heading" id="heading" size="30"> {!! $errors->first('heading', '<p class="red">:message

') !!} </td> </tr> <tr> <td>Image</td> <td><input type="file" name="image" id="image" size="40"> {!! $errors->first('image', '<p class="red">:message

') !!} </td> </tr> <tr> <td></td> <td colspan="2"><input type="submit" name="submit" value="Add Item" /></td> </tr> </table> </form>

<strong>controller part</strong>

<ol><li>Using Regular expression format:</li> </ol>

<strong>- I got error message, “ The image format is invalid” .</strong>

public function store(){ $this->validate(request(),[ 'heading'=>'required', 'contentbody'=>'required', ‘image'=>['required','image','mimes:jpeg,png,jpg,gif,svg','max:2048','regex:/^[a-z0-9-_]+$/' ] ]); } <ol start="2"><li>using Alpa_dash: </li> </ol>

<strong>- I got error message, “The image may only contain letters, numbers, and dashes” .</strong>

public function store(){ $this->validate(request(),[ 'heading'=>'required', 'contentbody'=>'required', 'image'=>'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048|alpha_dash' }

Please help, Thank you!


You need to use custom regex I reckon


and in your validation use like this

public function store(){ $this->validate(request(),[ 'heading'=>'required', 'contentbody'=>'required', ‘image'=>'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048|regex:/^[a-z0-9_.-]*$/' ]);

<strong>Edit 2:</strong>

I am not sure but as per docs laravel supports only the mentioned format. however I found no issue with jpg when I used validation for images but still lets this out too..

<a href="https://laravel.com/docs/5.1/validation#available-validation-rules" rel="nofollow">docs</a>

<strong>EDIT 3</strong>

for custom validation see <a href="https://laravel.com/docs/5.1/validation#custom-validation-rules" rel="nofollow">here</a>


If somebody else has the same problem like me. I solved my problem by changing the filename to the current time-stamp instead of using the original filename. That way, I don't need to be worried about the validation of the original filename to be saved in the database.

public function store(Request $request) { $this->validate($request,[ 'heading'=>'required', 'contentbody'=>'required', 'image'=>['required','image','mimes:jpg,png,jpeg,gif,svg','max:2048'] ]); if($request->hasFile('image')){ $inputimagename= time().'.'.$request->file('image')->getClientOriginalExtension(); $request->image->storeAs('public/upload', $inputimagename); Post::create([ 'heading'=>request('heading'), 'content'=>request('contentbody'), 'image'=>$inputimagename, ]); } return redirect('/home'); }


  • How To Solve This Login Error In Laravel
  • Form action not working in laravel
  • How To Solve This Data Update Error In Laravel
  • How to update data to database using controller in Laravel - Error Non-static method
  • Export DataTable to Excel asp
  • HTML rendering in Outlook 2010 and Gmail
  • Matching specific table within HTML, BeautifulSoup
  • Using jquery ajax json format, How do you output a query from a .cfm page to the calling html page?
  • The script does not work in IE. How can I fix it?
  • jQuery: remove the closest with a dynamically added button
  • Error css: vertical align in td with input text and image
  • Bootstrap-datetimepicker - dynamically add shows up in wrong area
  • width of column in rich:datatable
  • displaying space padded records in asp.net gridview
  • Submitting two different forms with an external Submit button not working properly
  • Can't send file with ajax to php file
  • Why does the font in these TD elements render at different sizes?
  • garbled css name when styling within UiBinder
  • How to open html table in xls on click of a button
  • how to get username into sql trigger when multiple users signed on from asp membership
  • How to change the font size of a single index for UISegmentedControl?
  • Custom validator control occupying space even though display set to dynamic
  • Read a local file using javascript
  • Highlight and Bold text in JTextPane
  • Change multiple background-images with jQuery
  • Xamarin Forms - UWP Fonts
  • Android screen density dpi vs ppi
  • Submit form in a displaytag pagination
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Change an a tag attribute in JavaScript based on screen width
  • Delete MySQLi record without showing the id in the URL
  • Unanticipated behavior
  • Comma separated Values
  • How to delete a row from a dynamic generate table using jquery?
  • Angular 2 constructor injection vs direct access
  • Trying to get generic when generic is not available
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • UserPrincipal.Current returns apppool on IIS