Validating a file upload
If we want to allow users to upload a file through our web form, we may want to restrict which kind of file they upload. Using Laravel's Validator
class, we can check for a specific file type, and even limit the upload to a certain file size.
Getting ready
For this recipe, we need a standard Laravel installation, and an example file to test our upload.
How to do it...
Follow these steps to complete this recipe:
Create a route for the form in our
routes.php
file:Route::get('fileform', function() { return View::make('fileform'); });
Create the form view:
<h1>File Upload</h1> <?php $messages = $errors->all('<p style="color:red">:message</p>') ?> <?php foreach ($messages as $msg) { echo $msg; } ?> <?= Form::open(array('files' => TRUE)) ?> <?= Form::label('myfile', 'My File (Word or Text doc)') ?> <br> <?= Form::file('myfile') ?> <br> <?= Form::submit('Send it!') ?> <?= Form::close() ?...