简体   繁体   English

如何将文件从内存存储到Laravel5中的存储中?

[英]How to store file from memory to a storage in Laravel5?

I have a variable, which holds an image as a base64 string. 我有一个变量,将图像保存为base64字符串。 How is it possible to put that file to a storage? 如何将该文件放入存储设备? I tried this: 我尝试了这个:

\Illuminate\Support\Facades\Storage::disk($disk_name)->putFileAs($dir_path, base64_decode($image_str), $file_name);

Unfortunately I get this error: Call to a member function getRealPath() on string 不幸的是,我收到此错误: Call to a member function getRealPath() on string

The reason is, that the second parameter of the putFile function should be an Illuminate\\Http\\File object, but I have no idea how to create one from a base64 string. 原因是, putFile函数的第二个参数应该是Illuminate\\Http\\File对象,但是我不知道如何从base64字符串创建一个。

You could do something like this: 您可以执行以下操作:

Storage::disk($disk_name)->put($dir_path . '/' . $file_name, base64_decode($image_str));

See the Laravel Storage docs . 请参阅Laravel存储文档

The put method accepts the path of the file as the first parameter and the contents as the second parameter. put方法将文件的路径作为第一个参数,将内容的路径作为第二个参数。

Note: if your $dir_path already includes a / , remove that from the function so there's not doubles. 注意:如果$dir_path已经包含/ ,请将其从函数中删除,这样就不会出现双精度的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM