Step 1

First, create a view file called resources/views/uploadfile.php where a user can select a file to be uploaded and copy the following code in that file.

<html>
   <body>
      <form 
       action="{{ route('uploadFile') }}" 
       method="post" 
       enctype="multipart/form-data">
	@csrf

	<input type="file" name="file">

	<button type="submit">Submit</button>
     </form>
   </body>
</html>

Step 2

Create a controller called UploadController by running laravel built-in awesome artisan command interface.

php artisan make:controller UploadController

<?php

namespace App\Http\Controllers;

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

class UploadFileController extends Controller {
   public function index() {
      return view('uploadfile');
   }
   public function store(Request $request) {
      $file = $request->file('image');
   
      //Display File Name
      echo 'File Name: '.$file->getClientOriginalName();
      echo '<br>';
 
      //Display File Real Path
      echo 'File Real Path: '.$file->getRealPath();
      echo '<br>';
 
      //Move Uploaded File
      $destinationPath = 'uploads';
      $file->move($destinationPath,$file->getClientOriginalName());
   }
}

Step 3

Define following routes in app/routes/web.php

Route::get('/uploadfile','UploadController@index');
Route::post('/uploadfile','UploadController@store');

Step 4

Visit the following URL to test the upload file.

http://localhost:8000/uploadfile

Author

I am a Laravel developer with some other skills.

Write A Comment