Anastasie Laurent
Anastasie Laurent

Reputation: 1179

laravel catch image in controller

This is my html code

{{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() }}

and in my controller I do this

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

I got an empty result

my question is how to catch the image in the controller and if you how to save it to my sql database? my column is from type longblob

Upvotes: 0

Views: 2481

Answers (1)

The Alpha
The Alpha

Reputation: 146191

You get an empty result because you have this:

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

There is no name but only an id so you need to add a name in your input like this:

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

Now you may use this:

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

Save the data in the database:

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

Now insert the $image in your database field and to show it in your view just use:

// 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 . '" />';

Upvotes: 1

Related Questions