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.
答案 0 :(得分:0)
使用表单提交文件时,必须设置添加属性
<form enctype="multipart/form-data" method="post" action="{{ route('addarticle.store') }}" class="contact_form text-center" id="contact_form">
到开始表单标签:
$photo_articles->PHOTO_ARTICLE = base64_encode(
file_get_contents($request->file('PHOTO_ARTICLE')->path())
);
否则,它将仅提交文件名称,而不提交文件本身。
然后在您的路线中,您只需要:
{{1}}