在Laravel5.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"通过看到不同的来源,我做了以下工作,但没有什么改变。
有什么想法吗?
发布于 2018-04-05 12:12:48
根据我们的讨论,您需要提供如下权限:
icacls "public" /grant USER:(OI)(CI)F /TUSER是你个人电脑的用户
此外,如果要将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));
}
}希望这对你有帮助!
发布于 2021-06-03 20:53:27
我们可以按以下方式存储文件/图像,而不是以icacls "public" /grant USER:(OI)(CI)F /T的形式授予权限
Storage::disk('public')->put('Items_attachments/'.$img_name, base64_decode($data));Items_attachments -是公共中的子文件夹
$img_name - base64字符串的名称
$data -是解码对象
https://stackoverflow.com/questions/49670746
复制相似问题