Home > Backend Development > PHP Tutorial > 大家有没有发现一个有关问题

大家有没有发现一个有关问题

WBOY
Release: 2016-06-13 13:00:52
Original
950 people have browsed it

大家有没有发现一个问题
当使用fread函数时,如果设置的文件指针指向一个ascii码小于33(十进制)的字符,则无论读取多长都读不出来,即得到一个长度为0的字符串。譬如下面情况:
当设置文件指针为18或者22时,
fseek($file,18,SEEK_SET);
$buffer=fread($file,4);
这个得到的$buffer长度为0
有没有不同看法啊

------解决方案--------------------
我试验过了,一切正常,没有发现你所说的状况,源码如下
$filename = "./1.gif";
$handle = fopen($filename, "rb+");
fseek($handle,12,SEEK_SET);
$buffer=fread($handle,4);
var_dump($buffer);
------解决方案--------------------
肯定是你哪里弄得不对了

//模拟一个文件<br />
$s = "424d6c0600000000000036000000280000001c00000014000000010018000000";<br />
$fp = tmpfile();<br />
fwrite($fp, pack('H*', $s));<br />
<br />
//以十六进制查看<br />
fseek($fp, 0);<br />
$i = 0;<br />
while($i < 32) {<br />
  printf('%02x ', ord(fgetc($fp)));<br />
  if((++$i % 16) == 0) echo PHP_EOL;<br />
}<br />
/*<br />
42 4d 6c 06 00 00 00 00 00 00 36 00 00 00 28 00 <br />
00 00 1c 00 00 00 14 00 00 00 01 00 18 00 00 00 <br />
*/<br />
<br />
//进入你的问题<br />
fseek($fp, 18);<br />
$t = fread($fp, 4);<br />
echo strlen($t), ' : ', bin2hex($t);<br />
/*<br />
4 : 1c000000<br />
*/
Copy after login

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