Photo Management with Laravel – File upload


In this tutorial, you will build a simple upload form and save the uploaded photo in the storage.

File Upload

Uploading photos and documents is one of the most common use-cases in web development. If you’re just started, you learn form creation, form post, following with file upload. If you’re familiar with the form, Laravel saves your time from repeating the code of moving the uploaded file from temp folder to application folder.

First of all, bootstrap the project.

Create a new project

Welcome page

Let’s get started.

Create resource controller

Laravel provides a handy command to generate a CRUD controller.

The built-in welcome page is meaningless here. Remove the default route and register a resourceful route to the controller.

Next, craft the user interface.

Craft user interface

You create a layout file which is used across the application.

Create a blade file which is extending the layout file. In the blade file, you add a form with a file input and a submit button.

Create a blade file for displaying a success message.

Then, setup the upload form.

Setup upload form

In a normal form, form submission is transferring string like value to the server. The photo is not a string like value. With the enctype attribute, you can encode the photo when submitting to the server.

Upload form

After submitting the form, you retrieve the uploaded photo.

Retrieve uploaded photo

You can access to the uploaded photo from the file method in Illuminate\Http\Request.

Retrieve the uploaded file and store into the folder named images in the storage folder.

Store file

By default, the store method will generate a unique ID to serve as the file name. Don’t worry. You can always store with your desired file name.

Store with custom file name

Use storeAs method, it accepts the second argument as the custom file name. You combine the file name with the extension. If not, the photo will store without any extension.

Store with custom file name

What’s next

Next tutorial will be talking about validating the uploading file.

If you haven’t subscribed to my email list on the homepage, do it now. I’ll notify you when the new tutorial is published.

Did I tell you that you’ll receive a series of FREE tutorials when you subscribe? Go check it out.

Stay tuned.

Also published on Medium.