【问题标题】:Laravel 5.5 Storing image from base64 stringLaravel 5.5 从base64字符串存储图像
【发布时间】:2018-04-05 11:01:04
【问题描述】:

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

我在下面看到以下错误:

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

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

  1. php artisan clear-compiled
  2. Icacls public /grant Everyone:F
  3. 作曲家转储自动加载

有什么想法吗?

【问题讨论】:

标签: laravel laravel-5


【解决方案1】:

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

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

USER 是您电脑的用户

另外,如果要将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));     
      }
}

希望对您有所帮助!

【讨论】:

    【解决方案2】:

    我们可以按如下方式存储文件/图像,而不是授予icacls "public" /grant USER:(OI)(CI)F /T 的权限

    Storage::disk('public')->put('Items_attachments/'.$img_name, base64_decode($data));
    

    Items_attachments - 是public里面的子文件夹

    $img_name - base64 字符串的名称

    $data - 是解码对象

    【讨论】:

      猜你喜欢
      • 2018-03-15
      • 1970-01-01
      • 2020-09-17
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-11
      • 1970-01-01
      相关资源
      最近更新 更多