通过php中的字节数获取文件的一部分 [英] Get part of a file by byte number in php

查看:89
本文介绍了通过php中的字节数获取文件的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能让php返回文件的一些字节?就像,我想加载字节7到字符串15,而不读取文件的任何其他部分?它的重要之处在于我不需要将所有的文件加载到内存中,因为这个文件可能相当大。 解决方案

使用 fseek() fread()

  $ fp = fopen('somefile.txt','r'); 

//移动到第7个字节
fseek($ fp,7);

$ data = fread($ fp,8); //从字节7读取8个字节

fclose($ fp);


How can I have php return just some bytes of a file? Like, it I wanted to load byte 7 through 15 into a string, without reading any other part of the file? Its important that I don't need to load all of the file into memory, as the file could be quite large.

解决方案

Use fseek() and fread()

$fp = fopen('somefile.txt', 'r');

// move to the 7th byte
fseek($fp, 7);

$data = fread($fp, 8);   // read 8 bytes from byte 7

fclose($fp);

这篇关于通过php中的字节数获取文件的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆