如何为Base64德在PHP code大文件 [英] How to base64-decode large files in PHP

查看:290
本文介绍了如何为Base64德在PHP code大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PHP Web应用程序具有可收到相当大的文件(最多32 MB),这是连接的base64 codeD的API。我们的目标是某处写我的文件系统的这些文件。德美元。当然C $ CD。什么是这样做的最少资源密集型的方式?

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?

编辑:通过API Recieving文件意味着我有一个32MB串在我的PHP应用程序,而不是一个32 MB源某处磁盘文件。我需要得到该字符串德codeD上的文件系统。

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.

使用PHP自身base64_de code()不割它,因为它采用了大量的内存所以我一直运行到PHP的内存限制(我知道,我不可能拿出这么限制,但我不觉得好有关允许PHP使用每个进程256MB左右)。

Using PHP's own base64_decode() isn't cutting it because it uses a lot of 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).

任何其他的选择吗?我可以做手工?或写恩codeD到磁盘文件并调用一些外部命令?任何想法?

Any other options? Could I do it manually? Or write the file to disk encoded and call some external command? Any thought?

推荐答案

尽管这已经是公认的答案,我有不同的建议。

Even though this has an accepted answer, I have a different suggestion.

如果您是从一个API拉的数据,你不应该存储在一个变量整个负载。使用curl或其他HTTP取程序可将数据自动存储在一个文件中。

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.

假设你通过一个简单的GET网址获取数据:

Assuming you are fetching the data through a simple 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);

好处:


  • 应该会更快(巨大的变数少复制)

  • 很少的内存开销

如果你必须从一个临时变量获取数据,我可以建议这个办法:

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

这篇关于如何为Base64德在PHP code大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆