Creating a file uploader
There may be times when we'd like the user to upload a file to our server. This recipe shows how Laravel can handle file uploads through a web form.
Getting ready
To create a file uploader, we need a standard version of Laravel installed.
How to do it...
To complete this recipe, follow these steps:
Create a route in our
routes.php
file to hold the form:Route::get('fileform', function() { return View::make('fileform'); });
Create the
fileform.php
View in ourapp/views
directory:<h1>File Upload</h1> <?= Form::open(array('files' => TRUE)) ?> <?= Form::label('myfile', 'My File') ?> <br> <?= Form::file('myfile') ?> <br> <?= Form::submit('Send it!') ?> <?= Form::close() ?>
Create a route to upload and save the file:
Route::post('fileform', function() { $file = Input::file('myfile'); $ext = $file->guessExtension(); if ($file->move('files', 'newfilename.' . $ext)) { return 'Success'; ...