Posted by Arjun on Wednesday 19th June 2019

How to Upload, download, remove Files to Amazon S3 Using Laravel

How to Upload Laravel Laravel 5 Download S3

Create a controller called UploadController.php with following code.

<code>&lt;?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UploadController extends Controller
{

    public function index()
    {
        $files = Storage::disk('s3')->files('files');

        $data = [];
        foreach($files as $file) {
            $data[] = [
                'name' => basename($file),
                'downloadUrl' => url('/download/'.base64_encode($file)),
                'removeUrl' => url('/remove/'.base64_encode($file)),
            ];
        }

        return view('upload', ['files' => $data]);
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'file' => 'required|max:2048'
        ]);

        if ($request->hasFile('file')) {
            $file = $request->file('file');
            $name = time() . $file->getClientOriginalName();
            $filePath = 'files/' . $name;
            Storage::disk('s3')->put($filePath, file_get_contents($file));
        }

        return back()->withSuccess('File uploaded successfully');
    }

    public function destroy($file)
    {
        $file = base64_decode($file);
        Storage::disk('s3')->delete($file);
        return back()->withSuccess('File was deleted successfully');
    }

    public function download($file) 
    {
        $file = base64_decode($file);
        $name = basename($file);
        Storage::disk('s3')->download($file, $name);
        return back()->withSuccess('File downloaded successfully');
    }
}
</code>

Create a view file

Create a file called upload.blade.php in the resources/views directory and place below inside it.

<code>&lt;div class="row">
               &lt;h3>Upload File&lt;/h3>
               &lt;div class="card col-sm-12">

                       &lt;form action="{{ url('/store') }}" method="POST" enctype="multipart/form-data" class="form-inline">
                          
                           {{ csrf_field() }}

                           &lt;div class="form-group">
                               &lt;label for="file">Select File&lt;/label>
                               &lt;input class="form-control-file" type="file" name="file" id="file">
                           &lt;/div>

                            &lt;button type="submit" class="btn btn-primary mb-2">Upload&lt;/button>

                       &lt;/form>
               &lt;/div>
               &lt;/div>
               
               &lt;div class="row">
               &lt;h3>Files&lt;/h3>
               &lt;div class="card col-sm-12">
                   @if (count($files) > 0)
                        @foreach ($files as $file)
                            &lt;a href="{{ url($file['downloadUrl']) }}">{{ $file['name'] }}&lt;/a>
                            &lt;form action="{{ url($file['removeUrl']) }}" method="POST">
                                {{ csrf_field() }}
                                {{ method_field('DELETE') }}
                                &lt;button type="submit" class="btn btn-default">Remove&lt;/button>
                            &lt;/form>
                        @endforeach
                   @else
                       &lt;p>Nothing found&lt;/p>
                   @endif
               &lt;/div>
           &lt;/div></code>

Define Routes

Open your routes/web.php directory and add following route definitions to it.

<code>Route::get('/', '[email protected]');
Route::post('/store', '[email protected]');
Route::get('download/{file}','[email protected]');
Route::delete('remove/{file}','[email protected]');</code>