Last active
August 29, 2015 14:13
-
-
Save steezeburger/4fda5bdff811c84fd2df to your computer and use it in GitHub Desktop.
Laravel controller for uploading files requiring certain mimetypes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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