how to save multiple images in table using laravel php

不想你离开。 提交于 2019-12-12 05:14:23

问题


Hi please help me am new to laravel I want to store multiple images in table... With this code am unable to save.

Help me for this..

Here in my View

{{Form::open(array('url'=>'businessdirectory/business', 'files'=>true))}}
{{Form::label('image','Upload Image')}}

<div class="form-group">{{Form::file('image[]',array('multiple'=>true))}} 

</div>
{{Form::close()}}

In my Controller

 if(Input::file('image'))
        {
        $image = Input::file('image');

        foreach($image as $img) {
        $destination = 'images';
        $filename = $img->getClientOriginalName();
        $path = 'images/'.$filename;
        $uploadSuccess = $img->move($destination,$filename);
        }
        }
        else
        {
            $path='images/default.JPG';
        }
 $business = new Business();    
 $business->image = $path;

回答1:


It's not advisable to store images on database. Just save the path of the images instead.

If you really need to store image on db. Make sure you set the column to blob as it need more space. Then, get the image content and type, then save it.

<?php
$image = fopen($image_path, 'rb');
// or
$image = file_get_contents($image_path);

$business = new Business;
$business->image = $image;
$business->imageType = "image/gif"; // 
$business->save();

// ...



回答2:


You need to put below code inside the foreach loop. In every loop you need to insert image path inside a database

 $business = new Business();    
 $business->image = $path;

This is a working code in laravel 5.2.

$files = $request->file('file');
 if($request->hasfile('file')){ 
    $destinationPath = 'uploads';
    foreach($files as $file){
        $image = new Image;
        $filename = $file->getClientOriginalName();
        $image->name = $filename;
        $image->save(); 
        $file->move($destinationPath,$filename);
    }


来源:https://stackoverflow.com/questions/28719584/how-to-save-multiple-images-in-table-using-laravel-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!