Laravel 5.5 从 base64 字符串存储图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49670746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:35:55  来源:igfitidea点击:

Laravel 5.5 Storing image from base64 string

laravellaravel-5

提问by Niladri Banerjee - Uttarpara

In Laravel 5.5 project, I have successfully saved the info into product table in MySQL. The info include a base64 string which is an image basically. However, I'm facing an issue while stroing the image in public folder of the laravel project. Below is my code for the ProductController.php

在 Laravel 5.5 项目中,我已成功将信息保存到 MySQL 的产品表中。信息包括一个 base64 字符串,它基本上是一个图像。但是,我在 Laravel 项目的公共文件夹中处理图像时遇到了问题。下面是我的 ProductController.php 代码

public function update(Request $request, $id)
{
    $data = $request->validate([
        'product_name' => 'required',
        'description' => 'required',
        'rating' => 'required'
    ]);

    $uploaded_image = $request->input('uploaded_image');
    $data['uploaded_image'] = $uploaded_image['value']; // base64 string
    $product = Product::findOrFail($id);
    $product->update($data);
    // the data stored into the database with no issue

    $image_base64 = base64_decode($uploaded_image['value']);
    $path = public_path();
    $success = file_put_contents($path, $image_base64.".png");

    return response()->json($data);
}

I see the following error below:

我在下面看到以下错误:

message:"file_put_contents(C:\xampp\htdocs\laravel-api\public): failed to open stream: Permission denied"

By seeing different sources, I did the following, but nothing changed.

通过查看不同的来源,我做了以下事情,但没有任何改变。

  1. php artisan clear-compiled
  2. Icacls public /grant Everyone:F
  3. composer dump-autoload
  1. php artisan 清晰编译
  2. Icacls public /grant 大家:F
  3. 作曲家转储自动加载

Any idea?

任何的想法?

回答by Hiren Gohel

As per our discussion you need to give permissions like:

根据我们的讨论,您需要授予以下权限:

icacls "public" /grant USER:(OI)(CI)F /T

Where USERis your pc's user

USER你电脑的用户在哪里

Also, if you want to save base64 image in storage path then use the following code:

另外,如果要将 base64 图像保存在存储路径中,请使用以下代码:

//Function to save a base64 image in laravel 5.4
public function createImageFromBase64(Request $request){

      $file_data = $request->input('uploaded_image');
      //generating unique file name;
      $file_name = 'image_'.time().'.png';
      //@list($type, $file_data) = explode(';', $file_data);
      //@list(, $file_data)      = explode(',', $file_data);
      if($file_data!=""){
        // storing image in storage/app/public Folder
        \Storage::disk('public')->put($file_name,base64_decode($file_data));     
      }
}

Hope this helps you!

希望这对你有帮助!