Laravel在控制器中捕获图像


laravel catch image in controller

这是我的html代码

{{Form::open(array('route'=>'admins.changePicture', 'class' => 'mainInformationContrainer', 'method'=> 'POST', 'file' => true)) }}
<ul>
    <li>
        <label>Picture</label>
        <div class="oneInfo thumbnail">
            <img src="#" id="imageID">
        </div>
    </li>
    <li>
        <label>.</label>
        <div class="oneInfo" style="width: 200px; overflow: hidden">
            <span class="spanForFileInput">
                <input type="button" class="selectImage" value="Select Image" />
                <input type="file" value="Select Image" id="imgInp"/>
            </span>
            <input type="submit"  />
        </div>
    </li>
    <li>
        <label></label>
        <div class="oneInfo">
        </div>
    </li>
</ul>
{{ Form::close() }}

在我的控制器中,我输入

public function changePicture(){
    $image = Input::file('imageID');echo $image; exit;
}

我得到一个空的结果

我的问题是如何在控制器中捕获图像,如果你如何将其保存到我的SQL数据库?我的专栏来自类型longblob

你得到一个空的结果,因为你有这个:

<input type="file" value="Select Image" id="imgInp"/>

没有名称,只有一个id,所以你需要在你的输入中添加一个name,像这样:

<input type="file" value="Select Image" id="imgInp" name="imgInp" />

现在你可以这样写:

$image = Input::file('imgInp');

将数据保存在数据库中:

$image = base64_encode(file_get_contents(Input::file('imgInp')->getRealPath()));

现在在数据库字段中插入$image,并在视图中显示它,只需使用:

// Assumed the field in the database is imageField
$imgdata = base64_decode($model->imageField);
$f = finfo_open();
$ext = finfo_buffer($f, $model->imageField, FILEINFO_MIME_TYPE);
echo '<img src="data:image/' . $ext . ';base64,' . $model->imageField . '" />';