Skip to content

Instantly share code, notes, and snippets.

@earth774
Created March 12, 2019 05:07
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save earth774/b4e75e12aae11709105beba2479de3e7 to your computer and use it in GitHub Desktop.
Save earth774/b4e75e12aae11709105beba2479de3e7 to your computer and use it in GitHub Desktop.
create upload image in lumen
<?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');
}
}
@NickAlvesX
Copy link

Just what I needed. Thank you!

@senocak
Copy link

senocak commented Nov 30, 2020

Thanks bro

@narek11
Copy link

narek11 commented Dec 23, 2020

Thank you

@aqibaja
Copy link

aqibaja commented Feb 19, 2021

thanks bro, very helpfully

@placecodex
Copy link

thanks bro

@stevebaros
Copy link

Thanks simple and straight to the point

@yoantz
Copy link

yoantz commented Jan 17, 2022

Thank you very much, tested on version 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment