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 - 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';
		$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.


The following two tabs change content below.

Maks Surguy

Full stack web developer, speaker and writer.
Maks is young and energetic breakdancer turned into web developer who lives in Seattle area with his wife. He is well-versed in three languages (English, Russian, Ukrainian) and a dozen of programming languages.