Uploading files in Laravel 4

This is a super short post about uploading files with Laravel 4.

I have struggled a few minutes trying to convert this from L3 format to L4 and to save somebody time decided to post this short PHP script.

This script can work with multi file uploads that I have documented in my previous blog post on AJAX file uploads – https://maxoffsky.com/code-blog/howto-ajax-multiple-file-upload-in-laravel/ basically adapting that method to the new Laravel 4 file operations.

In your controller where you POSTing your file upload, insert the following code:

$file = Input::file('file');
$destinationPath = 'uploads';
// If the uploads fail due to file system, you can try doing public_path().'/uploads' 
$filename = str_random(12);
//$filename = $file->getClientOriginalName();
//$extension =$file->getClientOriginalExtension(); 
$upload_success = Input::file('file')->move($destinationPath, $filename);

if( $upload_success ) {
   return Response::json('success', 200);
} else {
   return Response::json('error', 400);

This will give your file a random filename and  save it in the uploads folder.



I wrote a lengthy chapter of a book about AJAX file uploads and included a server-side example built with Laravel. Check it out here: http://leanpub.com/frontend

You may also like

More in Code Blog
AngularJS vs BackboneJS- initial impressions

A couple days ago I had to give a small presentation on AngularJS framework at my job. In this post...