PHP 流意外 0x0A [英] PHP streams unexpected 0x0A
问题描述
我正在处理一个关于流的非常奇怪的问题,我相信它是由 stream_copy_to_stream 引起的,但我不太确定.
I'm dealing with a pretty strange issue regarding streams, I believe it's caused by stream_copy_to_stream but I'm not really sure.
伪代码:
$reader = fopen($in_file, 'r'); // tried with rb as well
read X bytes from $reader and process the data
$writer = fopen($out_file, 'w'); // tried with wb as well
$filter = stream_filter_append($reader, 'filter_name', STREAM_FILTER_READ, $options);
stream_copy_to_stream($reader, $writer);
会发生什么:假设在调用 stream_copy_to_stream 时 $reader 中还剩下 N 个字节.生成的文件 $out_file 将有 N 个字节,但 0x0A 将被添加到文件中,最后一个字节将被忽略.示例:
What happens: Let's say that there are N bytes left in $reader when stream_copy_to_stream is called. The resulting file $out_file will have N bytes, but a 0x0A will be prepended to the file, and the last byte will be ignored. Example:
original file start: ffd8 ffe0 0010 4a46 4946 0001
original file end: b3c1 c557 92bb 3fff d9
resulting file start: 0aff d8ff e000 104a 4649 4600 01
original file end: b3 c1c5 5792 bb3f ff
起初,我能够通过在调用 stream_copy_to_stream() 之前执行 ob_flush() 来解决这个问题.但是现在我从命令行,从队列(使用 Laravel/Artisan/Beanstalkd)运行代码,这不再起作用.无论文件有多大,问题都会显现出来.我真的不知道这是什么原因造成的.
At first, I was able to fix this problem by executing an ob_flush() before the stream_copy_to_stream() call. But now I'm running the code from the command line, from a queue(using Laravel/Artisan/Beanstalkd) and this doesn't work anymore. No matter how large the file is, the problem manifests. I really have no idea what's causing this.
在 Windows 和 Linux、php 5.5 和 5.6 上都试过了.
Tried on both Windows and Linux, php 5.5 and 5.6.
推荐答案
该问题是由将标头发送到浏览器后传递的换行符引起的,可能是由我在项目中使用的包注入的.我需要做的就是找到导致此问题的文件.
The problem is caused by a newline character passed after the headers are sent to the browser, injected probably by a package I'm using in my project. All I need to do know is find the file causing this.
问题解决了.
这篇关于PHP 流意外 0x0A的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!