Created
March 12, 2019 05:07
-
-
Save earth774/b4e75e12aae11709105beba2479de3e7 to your computer and use it in GitHub Desktop.
create upload image in lumen
This file contains hidden or 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 | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Laravel\Lumen\Routing\Controller as BaseController; | |
class Controller extends BaseController | |
{ | |
public function uploadImage(Request $request) | |
{ | |
$response = null; | |
$user = (object) ['image' => ""]; | |
if ($request->hasFile('image')) { | |
$original_filename = $request->file('image')->getClientOriginalName(); | |
$original_filename_arr = explode('.', $original_filename); | |
$file_ext = end($original_filename_arr); | |
$destination_path = './upload/user/'; | |
$image = 'U-' . time() . '.' . $file_ext; | |
if ($request->file('image')->move($destination_path, $image)) { | |
$user->image = '/upload/user/' . $image; | |
return $this->responseRequestSuccess($user); | |
} else { | |
return $this->responseRequestError('Cannot upload file'); | |
} | |
} else { | |
return $this->responseRequestError('File not found'); | |
} | |
} | |
protected function responseRequestSuccess($ret) | |
{ | |
return response()->json(['status' => 'success', 'data' => $ret], 200) | |
->header('Access-Control-Allow-Origin', '*') | |
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); | |
} | |
protected function responseRequestError($message = 'Bad request', $statusCode = 200) | |
{ | |
return response()->json(['status' => 'error', 'error' => $message], $statusCode) | |
->header('Access-Control-Allow-Origin', '*') | |
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); | |
} | |
} |
Thanks bro
Thank you
thanks bro, very helpfully
thanks bro
Thanks simple and straight to the point
Thank you very much, tested on version 8
thank you bro!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just what I needed. Thank you!