PHP:读取的字节数
假设我读取了这样的字节数:
$data = fread($fp, 4096);
由于 fread
如果到达文件末尾就会停止读取,我如何才能准确知道读取了多少字节? strlen($data)
有效吗?或者这可能是错误的?
我想要完成的是读取多个字节,然后返回到读取之前的位置。我试图避免使用算术(ftell
、fread
、ftell
、subract、fseek
),因为文件可能大于 PHP_INT_MAX
并且可能会造成混乱。我想要的只是执行 fseek($fp, -$bytes_read, SEEK_CUR)
,但为此我需要知道我刚刚读取了多少字节......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
fread
之后使用ftell($fp)
< /a> 获取当前文件位置。After
fread
useftell($fp)
to get the current file position.检查这一点(未经测试):
第二个参数
'8bit'
强制函数返回字节数。在 mb_strlen 的 php 手册 的评论中找到。
Check this (untested):
The second argument
'8bit'
forces the function to return the number of bytes.Found in comments at php manual for mb_strlen.