Home > Backend Development > PHP Tutorial > PHP读取图片,读取到的字节比图片实际字节多了,仔细研读了PHP手册自认为代码没有关问题

PHP读取图片,读取到的字节比图片实际字节多了,仔细研读了PHP手册自认为代码没有关问题

WBOY
Release: 2016-06-13 13:01:28
Original
1110 people have browsed it

PHP读取图片,读取到的字节比图片实际字节多了,仔细研读了PHP手册自认为代码没问题
我的Code如下:

<?php<br />
 <br />
  $filepath="c:\123.jpg";<br />
  $filesize=filesize($filepath);<br />
  $fs=fopen($filepath,"r");<br />
  $readlen=1024;//每次读取多少字节<br />
  $rlen=0;//共读取多少字节<br />
  while(!feof($fs)){<br />
    	$data=fread($fs,$readlen);<br />
    	$rlen+=$readlen;<br />
   }<br />
   fclose($fs);<br />
   echo '文件大小'.$filesize.'<br/>';<br />
   echo '读取大小'.$rlen.'<br/>';<br />
?>
Copy after login

PHP朋友们给看看,我电脑系统windows 7的,这应该与系统无关系.
------解决方案--------------------
这恰恰与系统有关!
window 的文件打开方式有文本方式和二进制方式的区别
文本方式下,会对每个 \n (不是 \r\n 组合)附加一个 \r,形成 \r\n
所以字节数就多出来了

你需要用二进制防止打开图片
$fs=fopen($filepath,"rb");

或者直接用 file_get_contents

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template