How to upload files in Laravel directly into the public folder?


This is what we have in our public/ folder.

Let us move the files uploaded inside images/ folders in public. The file upload display is as follows −

The blade template for the above is as follows −

<html> <head> <title>Student Form</title> </head> <body> @if (count($errors) > 0) <div class = "alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <?php echo Form::open(array('url'=>'/student', 'files'=>'true')); echo 'Upload File.'; echo Form::file('image'); echo Form::submit('Upload'); echo Form::close(); ?> </body> </html>

Example 1

Now upload a file and see if the changes are in the public folder.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class StudentController extends Controller { public function index() { return view('student'); } public function uploadFile(Request $request) { //Move Uploaded File to public folder $destinationPath = 'images'; $myimage = $request->image->getClientOriginalName(); $request->image->move(public_path($destinationPath), $myimage); } }

To move the file to the public folder we are making use of $request->image->move() which helps to move the image uploaded to the destination path. The public folder path is retrieved using : public_path($destinationPath)

Output

The output of the above code is as follows −

Once you click on upload the file is moved to the public/ folder as shown below −

Example 2

You can make use of the store() method that is available on the file instance object. For store() you have to mention the path where you want to store the file.

Another important thing about store() method is that it creates a random unique name for the file stored and returns the path. The file is stored inside storage/public/images as shown below −

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; //use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\DB; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Storage; use URL; use Illuminate\Support\Facades\App; class StudentController extends Controller{ public function index() { return view('student'); } public function uploadFile(Request $request) { $file = $request->file('image'); echo $path = $request->file('image')->store('images'); } }

The storage/public/ folder details are as follows −

Output

The output of the above program is as follows −

The above path can be used to store the file location in your database.

Example 3

Another way to store the uploaded file is to make use of Storage::putfile() method. To make use of the Storage Facade class you need to include the following class:

use Illuminate\Support\Facades\Storage;

The putfile() method will store the file given in the folder mentioned. It will update the same in the storage/public folder.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; class StudentController extends Controller { public function index() { return view('student'); } public function uploadFile(Request $request) { $file = $request->file('image'); echo $path = Storage::putFile('images', $request->file('image')); } }

Output

The output of the above code is as follows

Inside storage/public/images/ folder you should see the file uploaded −

Example 4

If you see store() and putfile() change the name of the file as per their way. In case you don't want the file name to be changed you can make use of storeAs() method.

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; //use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\DB; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Storage; use URL; use Illuminate\Support\Facades\App; class StudentController extends Controller { public function index() { return view('student'); } public function uploadFile(Request $request) { echo $path = $request->file('image')->storeAs( 'images', $request->image->getClientOriginalName() ); } }

Output

The output of the above code is −

Inside storage/public/images folder you will see the file uploaded −

Updated on: 30-Aug-2022

25K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements