【问题标题】:how can i save my image as base 64 into database如何将我的图像作为 base 64 保存到数据库中
【发布时间】:2019-08-29 16:51:04
【问题描述】:

我正在尝试将图像从表单保存到数据库中。我试过了,但它不起作用:

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;
}

这是我的表格:

<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>

我的图片在$request-&gt;PHOTO_ARTICLE

谁能告诉我如何将它保存为base64?我搜索了很多,但没有结果。

【问题讨论】:

  • 只是出于好奇,为什么要将图像保存到数据库中,而不是将它们保存到文件系统中并在数据库中添加图像的路径?
  • 你得到什么错误?
  • @RossWilson 我正在做一个项目,他们指定所有内容都应保存在数据库中
  • 这是学校项目还是企业项目?
  • 仍然无法加载您的图像,因为 php 没有找到它。确保图像的路径正确。错误“没有这样的文件或目录”是不言自明的。

标签: php laravel base64


【解决方案1】:

当您提交带有表单的文件时,您必须将属性
enctype="multipart/form-data" 添加到打开的表单标签中:

<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())
);

【讨论】:

  • 没有错误。这就是我在视图中显示它的方式 PHOTO_ARTICLE)) }}" alt=""> 加载到数据库中的照片但是我认为它已损坏,因为当我显示它时什么都没有显示,但是当我从 phpmyadmin 手动加载我的照片时它会显示。
  • @OthmaneMessaoud 我已经更新了我的答案。这应该会给你你想要的。
猜你喜欢
  • 2017-03-15
  • 2018-08-10
  • 1970-01-01
  • 2011-07-20
  • 1970-01-01
  • 2015-12-17
  • 1970-01-01
  • 2020-04-07
相关资源
最近更新 更多