Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

In this tutorial, i will show you to upload files in laravel 7&8. we will create simple file upload like pdf, image, xlx, zip etc ...

In this example, we will create two routes one for get method and another for post method. we created simple form with file input. So you have to simple select file and then it will upload in "uploads" directory of public folder.

So you have to simple follow bellow step and get file upload in laravel 7&8 application.

Create Routes

In next step, we will add new two routes in routes/web.php file.

Route::get('/file-upload', 'FileUploadController@fileUpload')->name('file.upload');
Route::post('/file-upload', 'FileUploadController@fileUploadPost')->name('file.upload.post');

 

Create FileUploadController.php

In this step we will create new app/Http/Controllers/FileUploadController.php and here we have to write two method fileUpload() and fileUploadPost().

So one method will handle get method another one for post. So let's add code.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FileUploadController extends Controller
{

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function fileUpload()
    {
        return view('files.upload');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function fileUploadPost(Request $request)
    {
        $request->validate([
            'file' => 'required|mimes:pdf,csv|max:2048',
        ]);

        $fileName = time().'.'.$request->file->extension();

        $request->file->move(public_path('uploads'), $fileName);

        return back()->with('success','File uploaded successfully.')->with('file',$fileName);

    }
}

 

Create Blade Files

At last step we need to create resources/views/files/upload.blade.php file and in this file we will create form with file input button. So copy bellow and put on that file.

 

<!DOCTYPE html>
<html>
<head>
    <title>Upload file in laravel 7</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
</head>

<body>
<div class="container">
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h2>Example How to upload file in laravel 7</h2>
        </div>
        <div class="panel-body">

            @if ($message = Session::get('success'))
                <div class="alert alert-success alert-block">
                    <button type="button" class="close" data-dismiss="alert">×</button>
                    <strong>{{ $message }}</strong>
                    <p>
                        <a href="/uploads/{{ Session::get('file') }}" target="_blank">Download</a>
                    </p>
                </div>

            @endif

            @if (count($errors) > 0)
                <div class="alert alert-danger">
                    <strong>Whoops!</strong> There were some problems with your input.
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif

            <form action="{{ route('file.upload.post') }}" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="row">
                    <div class="col-md-6">
                        <input type="file" name="file" class="form-control">
                    </div>
                    <div class="col-md-6">
                        <button type="submit" class="btn btn-success">Upload</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
</html>

 

upload files in laravel 7

 

I hope you found this article useful. let me know if you have any questions and I’ll be happy to answer them.

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon