file_get_contents 或 fopen 用于读取几 MB 的 php://input?
这似乎是一个重复的问题,但事实并非如此:我通过 php:/input (1-500mb) 收到了几兆字节的数据,我必须将其保存在文件中。使用以下命令更能提高性能(服务器负载、速度):
file_put_contents($filename, file_get_contents('php://input'))
OR
$input = fopen("php://input", "r");
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
$target = fopen($filename, "w");
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);
This might seem to be a repeated question, but it is not: I receive several megabytes of data via php:/input (1-500mb) that I have to save in a file. Is more perfomance-wise (server load, speed) using :
file_put_contents($filename, file_get_contents('php://input'))
OR
$input = fopen("php://input", "r");
$temp = tmpfile();
$realSize = stream_copy_to_stream($input, $temp);
fclose($input);
$target = fopen($filename, "w");
fseek($temp, 0, SEEK_SET);
stream_copy_to_stream($temp, $target);
fclose($target);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
还有一个较短的版本:
copy
PHP 已经在内部实现了您的代码的功能。 (不确定这会产生可衡量的差异)。虽然我不确定为什么你要先创建一个临时文件。
如果输入文件最大为 500 MB,则
file_get_contents
方法无论如何都不起作用,因为它必须将所有数据保存在字符串/内存中。There is a shorter version still:
copy
PHP already internally implements what your code does. (Not sure it would make a measurable difference). Albeit I'm uncertain why you'd create a temporary file first.
And if the input file is up to 500 MB, then the
file_get_contents
approach wouldn't work anyway, as it had to keep all that data in a string / memory.