如何在 PHP 中对大文件进行 base64 解码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/888461/
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
How to base64-decode large files in PHP
提问by Sander Marechal
My PHP web application has an API that can recieve reasonably large files (up to 32 MB) which are base64 encoded. The goal is to write these files somewhere on my filesystem. Decoded of course. What would be the least resource intensive way of doing this?
我的 PHP Web 应用程序有一个 API,可以接收 base64 编码的相当大的文件(最多 32 MB)。目标是将这些文件写入我的文件系统中的某处。当然是解码了。这样做的资源密集程度最低的方法是什么?
Edit:Recieving the files through an API means that I have a 32MB string in my PHP app, not a 32 MB source file somewhere on disk. I need to get that string decoded an onto the filesystem.
编辑:通过 API 接收文件意味着我的 PHP 应用程序中有一个 32MB 的字符串,而不是磁盘上某处的 32MB 源文件。我需要将该字符串解码到文件系统上。
Using PHP's own base64_decode() isn't cutting it because it uses a lotof memory so I keep running into PHP's memory limit (I know, I could raise that limit but I don't feel good about allowing PHP to use 256MB or so per process).
使用 PHP 自己的 base64_decode() 并没有削减它,因为它使用了大量内存,所以我一直遇到PHP 的内存限制(我知道,我可以提高这个限制,但我对允许 PHP 使用 256MB 左右感觉不太好每个进程)。
Any other options? Could I do it manually? Or write the file to disk encoded and call some external command? Any thought?
还有其他选择吗?我可以手动完成吗?或者将文件写入磁盘编码并调用一些外部命令?任何想法?
回答by Evert
Even though this has an accepted answer, I have a different suggestion.
尽管这是一个公认的答案,但我有不同的建议。
If you are pulling the data from an API, you should not store the entire payload in a variable. Using curl or other HTTP fetchers you can automatically store your data in a file.
如果您从 API 中提取数据,则不应将整个有效负载存储在变量中。使用 curl 或其他 HTTP 提取器,您可以自动将数据存储在文件中。
Assuming you are fetching the data through a simple GET url:
假设您通过一个简单的 GET url 获取数据:
$url = 'http://www.example.com/myfile.base64';
$target = 'localfile.data';
$rhandle = fopen($url,'r');
stream_filter_append($rhandle, 'convert.base64-decode');
$whandle = fopen($target,'w');
stream_copy_to_stream($rhandle,$whandle);
fclose($rhandle);
fclose($whandle);
Benefits:
好处:
- Should be faster (less copying of huge variables)
- Very little memory overhead
- 应该更快(减少复制巨大变量)
- 很少的内存开销
If you must grab the data from a temporary variable, I can suggest this approach:
如果您必须从临时变量中获取数据,我可以建议这种方法:
$data = 'your base64 data';
$target = 'localfile.data';
$whandle = fopen($target,'w');
stream_filter_append($whandle, 'convert.base64-decode',STREAM_FILTER_WRITE);
fwrite($whandle,$data);
fclose($whandle);
回答by Gumbo
Decode the data in smaller chunks. Four characters of Base64 data equal three bytes of “Base256” data.
以较小的块解码数据。Base64 数据的四个字符等于“Base256”数据的三个字节。
So you could group each 1024 characters and decode them to 768 octets of binary data:
因此,您可以将每 1024 个字符分组并将它们解码为 768 个八位字节的二进制数据:
$chunkSize = 1024;
$src = fopen('base64.data', 'rb');
$dst = fopen('binary.data', 'wb');
while (!feof($src)) {
fwrite($dst, base64_decode(fread($src, $chunkSize)));
}
fclose($dst);
fclose($src);

