Hi
First of all Happy New Year to all of you.
Today we will learn how to upload multiple array of images in laravel.
When we build dynamic view to add images then we put our field name in array. Like below
For saving these kind of file or images we needed a controller in laravel.
Below is my controller code for uploading files and images:
Thanks.
First of all Happy New Year to all of you.
Today we will learn how to upload multiple array of images in laravel.
When we build dynamic view to add images then we put our field name in array. Like below
<input type="file" name="images[]" accept="image/*" class="input-group">
For saving these kind of file or images we needed a controller in laravel.
Below is my controller code for uploading files and images:
<?php
namespace App\Http\Controllers;
use Validator;
use Redirect;
use Request;
use Session;
class FileController extends Controller {
public function multiple_upload(Request $request) {
// getting all of the images form post data
$fileName=$request->file('images');
// Making counting of uploaded images
$file_count = count($fileName);
// $uploadcount = 0;
foreach($fileName as $file) {
$rules = array('images' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc': if we want to validate specific extension
$validator = Validator::make(array('images'=> $file), $rules);
if($validator->fails()){
$destinationPath = public_path("/images");
$file = $file->getClientOriginalName();
$upload_success = $file->move($destinationPath, $file);
$uploadcount ++;
}
}
if($uploadcount == $file_count){
Session::flash('success', 'Upload successfully');
return Redirect::to('your_route_name');
}
else {
return Redirect::to('your_route_name')->withInput()->withErrors($validator);
}
}
Thanks.