Image upload using lumen

lumen.png

To start, Laravel file systems don't work on lumen so just give up on using php artisan storage:link to link your storage. With that out of the way, let's learn how to store and read images in three easy steps.

STEP ONE: Install league/flysystems

Using composer require league/flysystem in your terminal to get the package.

STEP TWO: Integrate and use

in your lumen controller, for example userController.php use the following code in your create function

if ($request->hasFile('photo')) { $fileExtension = $request->file('photo')->getClientOriginalName(); $file = pathinfo($fileExtension, PATHINFO_FILENAME); $extension = $request->file('photo')->getClientOriginalExtension(); $fileStore = $file . '_' . time() . '.' . $extension; $path = $request->file('photo')->storeAs('photos', $fileStore); }

The storeAs() leads to the folder you'll be pushing the image to, for example storage/app/photos . Then you use the $fileStore variable to store in the database.

STEP THREE: Fetching the image

To fetch the image use;

$photo = $user['photo']; $path = "https://your_url/storage/app/photos/" . $photo; you can edit it to call on the current path and column and then return your response, that is all.

NOTE: This works when hosted on a server, local server requires using a storage_path and can only be viewed if you redirect to a resource/view page and call the $path in an img tag. ✌️

Learn Something New Everyday,
Connect With The Best Developers!

Sign Up Now!

& 500k+ others use Hashnode actively.

No Comments Yet