Laravel 5 amazon s3 file upload tutorial - Part 2

By Hardik Savani | January 4, 2017 | | 23330 Viewer | Category : PHP Laravel Bootstrap Amazon API




In this part we have last two step to create controller and make blade view file for file upload on amazon s3 server using Laravel 5 FileSystem.

In this step, we will use "Storage" facade for upload file in amazon server and also get cdn url of uploaded files. So let's follow bellow two step for finish the full example of upload image in s3 server.

Step 5: Create Controller

In this point, now we should create new controller as S3ImageController in this path app/Http/Controllers/S3ImageController.php. this controller will manage layout and image validation with post request, So run bellow command for generate new controller:

php artisan make:controller S3ImageController

Ok, now put bellow content in controller file:

app/Http/Controllers/S3ImageController.php

<?php


namespace App\Http\Controllers;


use Illuminate\Http\Request;

use Storage;


class S3ImageController extends Controller

{


/**

* Create view file

*

* @return void

*/

public function imageUpload()

{

return view('image-upload');

}


/**

* Manage Post Request

*

* @return void

*/

public function imageUploadPost(Request $request)

{

$this->validate($request, [

'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',

]);


$imageName = time().'.'.$request->image->getClientOriginalExtension();

$image = $request->file('image');

$t = Storage::disk('s3')->put($imageName, file_get_contents($image), 'public');

$imageName = Storage::disk('s3')->url($imageName);


return back()

->with('success','Image Uploaded successfully.')

->with('path',$imageName);

}

}

Step 6: Create View

In Last step, let's create image-upload.blade.php(resources/views/image-upload.blade.php) for layout and we will write design code here and also form for s3 image upload, So put following code:

resources/views/image-upload.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 5.3 Amazon S3 Image Upload with Validation example</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">

</head>

<body>


<div class="container">

<div class="panel panel-primary">

<div class="panel-heading"><h2>Laravel 5.3 Amazon S3 Image Upload with Validation example</h2></div>


<div class="panel-body">


@if (count($errors) > 0)

<div class="alert alert-danger">

<strong>Whoops!</strong> There were some problems with your input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


@if ($message = Session::get('success'))

<div class="alert alert-success alert-block">

<button type="button" class="close" data-dismiss="alert">×</button>

<strong>{{ $message }}</strong>

</div>

<img src="{{ Session::get('path') }}">

@endif


<form action="{{ url('s3-image-upload') }}" enctype="multipart/form-data" method="POST">

{{ csrf_field() }}

<div class="row">

<div class="col-md-12">

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

</div>

<div class="col-md-12">

<button type="submit" class="btn btn-success">Upload</button>

</div>

</div>

</form>

</div>


</div>

</div>


</body>

</html>

Now we are ready to run our example so run bellow command ro quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/s3-image-upload

I hope it can help you...




We are Recommending you: