doq1969 2014-07-31 13:24
浏览 48
已采纳

PHP读取错误的数据

I'm reading a binary file in PHP:

$File = fopen('simplefile', 'rb');
// Random stuff
fseek($File, $APosition);
for($I = 0; $I < 10; $I ++ ){
    var_dump(unpack('V', fread($File, 4)));
}
fclose($File);

However this does not give correct output for that position (it is giving 6357101 not 4294967295 as expected - I've checked using a hex editor).

However, this work and gives the correct values:

$File = fopen('simplefile', 'rb');
// Random stuff
fseek($File, $APosition);
for($I = 0; $I < 10; $I ++ ){
    fseek($File, ftell($File));  // What is this line doing?
    var_dump(unpack('V', fread($File, 4)));
}
fclose($File);

This however, I don't understand as surely fseek($File, ftell($File)); does absolutely nothing? What is that line doing and how can I read the correct values without errors?


This segment of the file is just 0xFF repeated several hundred times.

  • 写回答

2条回答 默认 最新

  • douren1891 2014-07-31 13:43
    关注

    I set up a file of all FF bytes, and the loop correctly reads -1 ten times, which is 4294967295 unsigned. I can't reproduce the value you get.

    The value you are getting, 6357101, corresponds to the bytes 6D 00 61 00. That is the characters 'ma' encoded in UTF-16, although it could also mean other things.

    I think the file or the content of the file is not what you think it is, or you are reading from the wrong place.

    fseek($File, ftell($File));  // What is this line doing?
    

    It should do nothing. If it doesn't do nothing I can only speculate that perhaps the file is corrupt on disk, or your copy of PHP is borked, or your computer has gone quietly mad.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题