PHP流意外0x0A

时间:2014-09-28 14:31:43

标签: php stream

我正在处理关于流的一个非常奇怪的问题,我相信它是由stream_copy_to_stream引起的,但我并不确定。

伪代码:

$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将被添加到文件中,最后一个字节将被忽略。例如:

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)运行代码,这不再起作用了。无论文件有多大,问题都会出现。我真的不知道造成这种情况的原因。

在Windows和Linux上尝试过,php 5.5和5.6。

1 个答案:

答案 0 :(得分:0)

问题是由标题发送到浏览器后传递的换行符引起的,可能是由我在我的项目中使用的包注入的。我需要知道的是找到导致此问题的文件。

问题解决了。

编辑:这是开幕前的换行符