How to save uploaded file in the server in Laravel

How to save uploaded file in the server in Laravel

laravel tutorials

laravel tutorials

This is the second part (back-end part) of my file uploading in Angular tutorial.

 
In this post I will show you how to save uploaded file in the server in Laravel. Also, I will use REST full API to get request from Angular front-end (client) to Laravel 5.5 back-end. You can find the first part (front-end) of this tutorial from here. click
Also, If you want to learn how to create a fresh Laravel application, click here.
 

When you uploading a file, it is transmitted from your computer to remote computer (or server). Angular application will get the file and send it to the Laravel back-end using http POST request.  So, In this tutorial you will,

.1. Get the file, access properties of that file and save it.

2. Then, Define route.

3. Finally, Check functionality

Get the file and access properties of that file.

Define route

Check functionality

Let’s start!

Get the file, access properties of that file and save it

First of all, we need to create a controller call, FileController. Run “php artisan make:controller File”. As a result, you can see new file controller in Controllers folder.

make the controller

make the controller

Then, open the FileController and create the method call, saveFile. So, the FileConroller is as follows:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FileController extends Controller
{
    public function saveFile(Request $request){
  
  $File = $request -> file('myfile'); //line 1
        $sub_path = 'files'; //line 2
        $real_name = $File -> getClientOriginalName(); //line 3
      
        $destination_path = public_path($sub_path);  //line 4
          
        $File->move($destination_path,  $real_name);  //line 5

        return response()->json('File Save');
    	 
    }
}

Line 1: get file from the request using the name (key) that was assigned in front-end part.

Line 2: specify the path that we want to store file. The target folder (here, files) should be in public folder in you Laravel application.

Notice: Here i’m working with local server.

file path

file path

Line 3: get the real name of the file.

Line 4: get the full path.

Line 5: finally, save the file in specified path.

Now, we are almost done.

Define route

Then, open the routes/api.php file and create new route. After that, the FileConroller is as follows:

<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Headers: *');

Route::post('file', 'FileController@saveFile');

Check functionality

Finally,  let me show you the functionality of both applications (front-end and back-end) as one application

 

So, I hope now you can save uploaded file in the server in laravel. If you have any problem, please comment in the comment section. If you like this post don’t forget to share.

Also, you can find completed project on github click here.

Thanks you for reading.

Summary
How to save uploaded file in the server in Laravel
Article Name
How to save uploaded file in the server in Laravel
Description
In this post I will show you how to save uploaded file in the server. Also, I will use REST full API to get request from Angular front-end (client) to Laravel 5.5 back-end. Angular application will get the file and send it to the Laravel back-end using http POST request.
Author
Publisher Name
Angular + Laravel
Publisher Logo
Please follow and like us: