Skip to content

Instantly share code, notes, and snippets.

@steezeburger
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steezeburger/4fda5bdff811c84fd2df to your computer and use it in GitHub Desktop.
Save steezeburger/4fda5bdff811c84fd2df to your computer and use it in GitHub Desktop.
Laravel controller for uploading files requiring certain mimetypes
<?php
class UploadController extends BaseController {
public function upload() {
// Getting POST data
$file = array('image' => Input::file('image'));
$rules = array('image' => 'required|mimes:jpeg,bmp,png',);
// doing the validation, passing post data, rules and the messages
$validator = Validator::make($file, $rules);
if ($validator->fails()) {
// send back to the page with the input data and errors
return Redirect::to('upload')->withInput()->withErrors($validator);
}
else {
// checking if file is valid.
if (Input::file('image')->isValid()) {
$destinationPath = public_path().'/storage/uploads'; // upload path
$extension = Input::file('image')->getClientOriginalExtension(); // getting image extension
$fileName = rand(11111,99999).'.'.$extension; // renaming image
Input::file('image')->move($destinationPath, $fileName); // uploading file to given path
// sending back with message
Session::flash('success', 'Upload successful');
return Redirect::to('/');
}
else {
// sending back with error message.
Session::flash('error', 'uploaded file is not valid');
return Redirect::to('upload');
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment