Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Image Upload in Laravel 5.2

Finally I can upload and move the images, but now I want to create a multiple upload images on Laravel. Is that possible? Did I have to use array to make it?

Can I just modify a little bit from this code?

It's on my ProductController.php

$picture = '';

if ($request->hasFile('images')) {
    $file = $request->file('images');
    $filename = $file->getClientOriginalName();
    $extension = $file->getClientOriginalExtension();
    $picture = date('His').$filename;
    $destinationPath = base_path() . '\public\images/';
    $request->file('images')->move($destinationPath, $picture);
}

if (!empty($product['images'])) {
    $product['images'] = $picture;
} else {
    unset($product['images']);
}

Thank you. Note: My code above is from a kindhearted person on stackoverflow, thanks again ;)

like image 873
Putri Dewi Purnamasari Avatar asked Apr 25 '16 05:04

Putri Dewi Purnamasari


1 Answers

At your frontend form you'll have to use your field attribute name like

name="images[]"

And your controller code would be like this.

$picture = '';
if ($request->hasFile('images')) {
    $files = $request->file('images');
    foreach($files as $file){
        $filename = $file->getClientOriginalName();
        $extension = $file->getClientOriginalExtension();
        $picture = date('His').$filename;
        $destinationPath = base_path() . '\public\images';
        $file->move($destinationPath, $picture);
    }
}

if (!empty($product['images'])) {
    $product['images'] = $picture;
} else {
    unset($product['images']);
}
like image 183
sohaib rehman Avatar answered Nov 11 '22 01:11

sohaib rehman