How to upload image in Laravel 8

How to upload image example in Laravel 8

Posted by Ervin Adams on June 5, 2021

How to upload image in Laravel 8

How to upload image example in Laravel 8

Posted by Ervin Adams on June 5, 2021

In this tutorial you will be shown how to upload an image or file to storage folder in Laravel using Laravel 8.

Step 1. Execute command to create controller

php artisan make:controller ImageUploadController

Edit the file in "app/Http/Controllers":

namespace App\Http\Controllers;

use Illuminate\Contracts\Foundation\Application;

use Illuminate\Http\RedirectResponse;

use Illuminate\Http\Request;

use Illuminate\Routing\Redirector;

class ImageUploadController extends Controller {

public function index() {

   return view('upload');

}

/**

* @param Request $request

* @return Application|RedirectResponse|Redirector

*/

   public function storeFile(Request $request) {

       $data = "File ".$request->file('file')->getClientOriginalName()." uploaded successfully. ";

       $request->file('file')->store('store-file');

       session()->flash('message', $data);

       return redirect('/');

   }

}

Step 2. add routes to web.php

Go to "routes/web.php" and add:

Route::get('/', [ImageUploadController::class, 'index']);

Route::post('/store-file',[ImageUploadController::class, 'storeFile'])->name('store.file');

Step 3. Create the view

Go to "resources/views" and create blade file "upload.blade.php"

<!DOCTYPE html> 
  <html> 
   <head> 
    <meta charset="utf-8"> 
    <title>Wecode101 receiving mail tutorial</title> 
    <meta content="Wecode101 upload file to storage folder in Laravel 8" name="description">
    <meta content="Wecode101" name="author"> <meta content="{{ csrf_token() }}" name="csrf-token">            <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"> 
   </head>
   <body>
      <div class="container mt-4"> 
        <div> Upload File 
          <form action="{{route('store.file')}}" method="post" enctype="multipart/form-data"> 
            <div class="form-group"> 
             <label for="exampleInputFile">File <input aria-describedby="File" id="exampleInputFile" name="file" type="file"></label> 
             <input aria-describedby="DataType" id="exampleInputDataType" value={{"bloomberg_name"}} name="data-type" type="hidden"> 
        </div> <div class="form-group"> <input name="_token" type="hidden" value="{{ csrf_token() }}"> <button class="btn btn-primary" type="submit">Submit</button> 
            </div> 
           </form>
         </div> 
       </div> 
     </body> 
 </html>

Step 4. Run command

php artisan serve

Go to the page and try to upload a file or an image. Now check "storage/app" directory, you should see "store-file" folder and the file should be there.

 If you wish to download this tutorial go to https://github.com/wecode101/image_or_file_upload

This field is required
Your question have been successfully submitted and will be reviewed before published
Please login to ask a question