Home  >  Article  >  Backend Development  >  readfile binary(图片)文件头会多出一位(0x0A)来.该怎么解决

readfile binary(图片)文件头会多出一位(0x0A)来.该怎么解决

WBOY
WBOYOriginal
2016-06-13 10:09:07877browse

readfile binary(图片)文件头会多出一位(0x0A)来.
大家好,现在我们的项目遇到了一个比较困扰的问题。使用readfile下载图片文件时,文件会文件首会比原来多出一位(0x0A)来,使用UEdit删除文件首的那个16进制位文件可正确读出。

源代码如下:

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php $file = 'test_img.bmp';if (file_exists($file)) {    header('Content-Description: File Transfer');    header('Content-Type: application/octet-stream');    header('Content-Disposition: attachment; filename='.basename($file));    header('Content-Transfer-Encoding: binary');    header('Expires: 0');    header('Cache-Control: must-revalidate');    header('Pragma: public');    header('Content-Length: ' . filesize($file));    ob_clean();    flush();    readfile($file);    exit;}?>



下载后的文件和原文件比较如下:


代码在以前的服务器上运行从未出现问题,但当我们的项目更换了运行支撑环境(操作系统/apache/php 版本均有可能不同),在网上查遍了所有的文档,无解决办法。求高手解决此问题,在此先感谢大家了!

如有有效解决办法,将给您追加到200分结贴。



------解决方案--------------------
PHP code
//给你一段//$mime,文件类型//$filename,这个不用说了吧//$data 是你要输出的文件数据,你试试用 file_get_contents 来获得if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE)        {            header('Content-Type: "'.$mime.'"');            header('Content-Disposition: attachment; filename="'.$filename.'"');            header('Expires: 0');            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');            header("Content-Transfer-Encoding: binary");            header('Pragma: public');            header("Content-Length: ".strlen($data));        }        else        {            header('Content-Type: "'.$mime.'"');            header('Content-Disposition: attachment; filename="'.$filename.'"');            header("Content-Transfer-Encoding: binary");            header('Expires: 0');            header('Pragma: no-cache');            header("Content-Length: ".strlen($data));        }        exit($data);<br><font color="#e78608">------解决方案--------------------</font><br>刚用了你的代码测试了一下,没出现你说的问题。<br><br>  ob_clean();<br>   flush();<br><br>把这两句去掉看看。<br><font color="#e78608">------解决方案--------------------</font><br>在你的这行代码前,看是否有输出过字符??<br><font color="#e78608">------解决方案--------------------</font><br>多出个换行符,,,贴的不是你生产环境的代码吧<br><font color="#e78608">------解决方案--------------------</font><br>源代码如下:<br><br>[code=PHP]<br><br><?php <br />$file = 'test_img.bmp';<br><br>很显然,您的 <?php 前有一个空行<br />更低级的就是你连 BOM 头都没有避讳<div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn