Laravel 5 amazon s3 file upload tutorial – Part 2

Laravel 5 amazon s3 file upload tutorial – Part 2

In this post we will give you information about Laravel 5 amazon s3 file upload tutorial – Part 2. Hear we will give you detail about Laravel 5 amazon s3 file upload tutorial – Part 2And how to use it also give you demo for it if it is necessary.

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 AppHttpControllers;


use IlluminateHttpRequest;

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 >

<div >

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


<div >


@if (count($errors) > 0)

<div >

<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 >

<button type="button" 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 >

<div >

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

</div>

<div >

<button type="submit" >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…

Hope this code and post will helped you for implement Laravel 5 amazon s3 file upload tutorial – Part 2. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  27  =  37

We accept paid advance and paid guest Posting on our Site : Contact US