-
Notifications
You must be signed in to change notification settings - Fork 1.6k
PHP Laravel 5 controller example
Sithdown edited this page Nov 9, 2015
·
9 revisions
A simple upload handler; validates that the file is an image.
Requires that a folder called "uploads" is made in the /storage, with the usual Write permissions.
namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Input;
use Validator;
class ImageController extends Controller {
/**
* Store an image.
*
* @return simple JSON response message
*/
public function store(Request $r)
{
$image = Input::file('file');
$validator = Validator::make([$image], ['image' => 'required']);
if ($validator->fails()) {
return $this->errors(['message' => 'Not an image.', 'code' => 400]);
}
$destinationPath = storage_path() . '/uploads';
if(!$image->move($destinationPath, $image->getClientOriginalName())) {
return $this->errors(['message' => 'Error saving the file.', 'code' => 400]);
}
return response()->json(['success' => true], 200);
}
}