对于我的项目,我需要从 BIG 文件(>3 GB)中的 BIG 偏移读取 10KB 的块。写入始终是附加的,因此不需要偏移量。
这将起作用,无论您使用的是哪个 PHP 版本和操作系统。
先决条件=您的服务器应该支持范围检索查询。Apache 和 IIS 已经支持这一点,其他 99% 的网络服务器(共享主机或其他)也是如此
// offset, 3GB+
$start=floatval(3355902253);
// bytes to read, 100 KB
$len=floatval(100*1024);
// set up the http byte range headers
$opts = array('http'=>array('method'=>'GET','header'=>"Range: bytes=$start-".($start+$len-1)));
$context = stream_context_create($opts);
// bytes ranges header
print_r($opts);
// change the URL below to the URL of your file. DO NOT change it to a file path.
// you MUST use a http:// URL for your file for a http request to work
// this will output the results
echo $result = file_get_contents('http://127.0.0.1/dir/mydbfile.dat', false, $context);
// status of your request
// if this is empty, means http request didnt fire.
print_r($http_response_header);
// Check your file URL and verify by going directly to your file URL from a web
// browser. If http response shows errors i.e. code > 400 check you are sending the
// correct Range headers bytes. For eg - if you give a start Range which exceeds the
// current file size, it will give 406.
// NOTE - The current file size is also returned back in the http response header
// Content-Range: bytes 355902253-355903252/355904253, the last number is the file size
...
...
...
安全 - 您必须添加一个 .htaccess 规则,该规则拒绝对该数据库文件的所有请求,但来自本地 ip 127.0.0.1 的请求除外。