简体   繁体   中英

how can i save my image as base 64 into database

I'm trying to save an image from a form into database. I tried this but it's not working:

public function store(Request $request)
{
    $article = new article;
    $photo_articles = new photo_articles;
    // $type = new type;

    $article->NOM_ARTICLE = $request->NOM_ARTICLE;
    $article->DESCRIPTION_ARTICLE = $request->DESCRIPTION_ARTICLE;
    $article->id = auth()->user()->id;
    $article->TYPE_ARTICLE = $request->LABEL_TYPE;
    $article->save();

    $photo_articles->PHOTO_ARTICLE = base64_encode(file_get_contents($request->PHOTO_ARTICLE));
    $photo_articles->ID_ARTICLE = $article->ID_ARTICLE;

    $photo_articles->save();

    return;
}

Here is my form:

<form method="post" action="{{ route('addarticle.store') }}" class="contact_form text-center" id="contact_form">
    {{ csrf_field() }}
    <div class="row">
        <div class="col-lg-6">
            <div class="col-lg-12">
                <input type="text" class="contact_input" name="NOM_ARTICLE" placeholder="Nom d'article"
                       required="required">
            </div>

            <div class="col-lg-12">
                <select class="contact_input" name="LABEL_TYPE">
                    @foreach($types as $type)
                        <option> {{$type->LABEL_TYPE}}</option>
                    @endforeach
                </select>
            </div>
        </div>
        <div class="col-lg-6">
            <div id="uploading" class="uploadfile">
                <input type="hidden" name="MAX_FILE_SIZE" value="250000"/>
                <input type="file" class="contact_input uploadFileInput" id="imagearticle" name="PHOTO_ARTICLE"
                       placeholder="Capture de votre article" name="fic" size=50 required="required"/>
                <p id="uploadtextid" class="uploadText">upload image</p>
                <img class="uploadImage" src="" id="displayedimage">
            </div>
        </div>

        <div class="col-lg-12">
            <textarea class="contact_textarea contact_input" name="DESCRIPTION_ARTICLE" placeholder="Description"
                      required="required"></textarea>
        </div>

        <button class="contact_button right" type="submit">Valider!</button>
    </div>
</form>

My image is in $request->PHOTO_ARTICLE .

Can someone show me how to save it as base64 ? I've searched a lot but without result.

When you're submitting a file with a form you have to set add the attribute
enctype="multipart/form-data" to the opening form tag:

<form enctype="multipart/form-data" method="post" action="{{ route('addarticle.store') }}" class="contact_form text-center" id="contact_form">

otherwise it will only submit the name of the file and not the file itself.

Then in your route you would just need:

$photo_articles->PHOTO_ARTICLE = base64_encode(
    file_get_contents($request->file('PHOTO_ARTICLE')->path())
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM