开发者

PHP: Amount of bytes fread

开发者 https://www.devze.com 2023-03-09 07:24 出处:网络
Say I read a number of bytes like 开发者_如何学运维this: $data = fread($fp, 4096); Since fread will stop reading if it reaches the end of the file, how can I know exactly how much was read? Would s

Say I read a number of bytes like 开发者_如何学运维this:

$data = fread($fp, 4096);

Since fread will stop reading if it reaches the end of the file, how can I know exactly how much was read? Would strlen($data) work? Or could that be potentially wrong?


What I'm trying to accomplish, is to read a number of bytes, and then go back to where I was before I read. And I'm trying to avoid using arithmetic (ftell, fread, ftell, subract, fseek), since a file could potentially be larger than PHP_INT_MAX and potentially mess that up. What I would want is to just do fseek($fp, -$bytes_read, SEEK_CUR), but for that I need to know how many bytes I just read...


After fread use ftell($fp) to get the current file position.


Check this (untested):

mb_strlen($data, '8bit')

The second argument '8bit' forces the function to return the number of bytes.

Found in comments at php manual for mb_strlen.

0

精彩评论

暂无评论...
验证码 换一张
取 消