How to Manage Laravel 5.4 Files Upload

Laravel is by far the most popular PHP Framework. It has significant numbers of active community members plus many updates and packages. In this short tutorial, will show you how to upload and manage Laravel 5.4 files upload. If you are new to Laravel, make sure you read our old articles about Laravel Framework.

You will learn the tips and technique on how to upload file, move files, give a unique name for upload files and more. 

How to Upload Files in Laravel 5.4

Firstly, let’s see how easy and straightforward it is to upload a file in Laravel and then we will manage and modify the uploaded file.

First, we must create a form with file input to let us upload our file.

{{Form::open(['route' => 'user.store', 'files' => true])}}

{{Form::label('user_photo', 'User Photo',['class' => 'control-label'])}}
{{Form::file('user_photo')}}
{{Form::submit('Save', ['class' => 'btn btn-success'])}}

{{Form::close()}}

 

Let’s Imagine that we are uploading a photo for our user. Here are the requirements for our user.

  • We have table in database called user ( default Laravel auth )
  • We have an extra column called user_photo in that table
  • We have User model ( with fillable properties )
  • We have UserController
  • Resourceful Controller for User

I hope you can manage the above requirements.

The above form will submit data to the store method in the controller ( as like other projects ). As you can see, we are using Laravel form build from LaravelCollective. Our opening form has an extra option for file => “true”. It will add the enctype=”multipart/form-data” to the form which will let us upload file.

How to Handle File Upload

Here is how we can handle file in our controller.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
/**
* Upload the avatar for the user.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{
$path = $request->file('user_photo')->store('avatars');

return $path;
}
}

 

Be default; It will upload the file to storage directory and create a folder called avatars if doesn’t exist. So far it was just like other tutorials, but how can we upload the to a public directory instead of storage and how to change the uploaded file name and give it a unique name?

How to Manage Laravel 5.4 Files Upload

Now, let’s move the file to the public folder and give it a unique name. That way you can access the file easily in your views.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
/**
* Upload the avatar for the user.
*
* @param Request $request
* @return Response
*/
public function store(Request $request)
{

// get current time and append the upload file extension to it,
// then put that name to $photoName variable.
$photoName = time().'.'.$request->user_photo->getClientOriginalExtension();

/*
talk the select file and move it public directory and make avatars
folder if doesn't exsit then give it that unique name.
*/
$request->user_photo->move(public_path('avatars'), $photoName);


}
}

That’s it. Now you can save the $photoName to the database as a user_photo field value. You can use asset(‘avatars’) function in your view and access the photos.

Conclusion

We hope you have learned how to manage Laravel 5.4 files upload. If you have any questions regarding Laravel and file upload, feel free to comment it bellow. 🙂

file uploadHow tolaravelLaravel 5Laravel FrameworkPHP
Comments (2)
Add Comment
  • Oliver Russell

    This is a really simple functionality for file upload. I think it is better to always allow the functionality of multiple file upload in Laravel. Also, your tutorial was created in 5.4. Laravel 5.5 and 5.6 are also out now. There are few changes in 5.4, so I am not sure this process this process will still be same for latest versions.