In this tutorial, you will learn how to upload files in your Laravel 9 application. It’s going to be a simple and easy-to-understand step-by-step guide.
Step 1: Download and install Laravel 9
Let’s create a brand new project with the below command
composer create-project laravel/laravel file-upload-app
Step 2: Create Controller called FileUploadController.php
Let’s create a controller called FileUploadController.php
with the artisan make command as shown below.
Step 3: Create a Request called FileUploadRequest.php
Let’s create a Request called FileUploadRequest.php
with the artisan make command as shown below.
Step 4: Add validation Rules in FileUploadRequest.php
Open your FileUploadRequest.php
file and upload validation rules as shown below.
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class FileUploadRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'file' => 'required|mimes:pdf,xlx,csv|max:2048',
];
}
}
Step 5: Add index and Store methods in FileUploadController.php
Now open your FileUploadController.php
file, then, add the index function to load the view file and store the method to handle the upload process.
<?php
namespace App\Http\Controllers;
use App\Http\Requests\FileUploadRequest;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
class FileUploadController extends Controller
{
public function index(): Factory|View|Application
{
return view('fileUpload');
}
public function store(FileUploadRequest $request): RedirectResponse
{
$file = $request->file('file');
$fileName = $file->getClientOriginalName();
$file->storeAs('uploads', $fileName); // store file with original name
// $file->store('uploads); // store file with random name
return back()
->with('success', 'File have been upload successfully.');
}
}
Step 6: Create a view file called fileUpload.blade.php with the below HTML
Now create a view file called fileUpload.blade.php, with a form to upload the file as shown below.
<!DOCTYPE html>
<html>
<head>
<title>Laravel 9 File Upload - arjunphp.com</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading text-center mt-5">
<h2>Laravel 9 File Upload - arjunphp.com</h2>
</div>
<div class="panel-body mt-5">
@if ($message = Session::get('success'))
<div class="alert alert-success alert-dismissible fade show mb-2" role="alert">
{{ $message }}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
@endif
<form action="{{ route('file.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="mb-3">
<label class="form-label" for="inputFile">Select File:</label>
<input
type="file"
name="file"
id="inputFile"
class="form-control @error('file') is-invalid @enderror">
@error('file')
<span class="text-danger">{{ $message }}</span>
@enderror
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary">Upload</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Step 7: Add new routes in the routes/web.php file
Route::get('uploadFile', [FileUploadController::class, 'index']);
Route::post('uploadFile', [FileUploadController::class, 'store'])->name('file.store');
Step 8: Start and test the application.
Now start the application $ php artisan serve
and access the application in your browser at http://localhost:8000/uploadFile
, you should get simpler output after uploading the file.