我有一个非常大的文件(大约 20GB),如何使用 fseek() 来跳转并阅读其内容。
代码如下所示:
function read_bytes($f, $offset, $length) {
fseek($f, $offset);
return fread($f, $length);
}
仅当 $offset < 2147483647 时结果才正确。
更新:我在 Windows 64 上运行,phpinfo - 架构:x64,PHP_INT_MAX:2147483647
我有一个非常大的文件(大约 20GB),如何使用 fseek() 来跳转并阅读其内容。
代码如下所示:
function read_bytes($f, $offset, $length) {
fseek($f, $offset);
return fread($f, $length);
}
仅当 $offset < 2147483647 时结果才正确。
更新:我在 Windows 64 上运行,phpinfo - 架构:x64,PHP_INT_MAX:2147483647
警告:如评论中所述,fseek 在内部使用 INT,它根本无法在 32 位 PHP 编译中处理如此大的文件。以下解决方案不起作用。留在这里仅供参考。
一点点搜索让我看到了关于 fseek 的 PHP 手册页的评论:
http://php.net/manual/en/function.fseek.php
问题是偏移参数的最大 int 大小,但似乎您可以通过使用 SEEK_CUR 选项进行多次 fseek 调用并将其与大数字处理库之一混合来解决它。
例子:
function fseek64(&$fh, $offset)
{
fseek($fh, 0, SEEK_SET);
$t_offset = '' . PHP_INT_MAX;
while (gmp_cmp($offset, $t_offset) == 1)
{
$offset = gmp_sub($offset, $t_offset);
fseek($fh, gmp_intval($t_offset), SEEK_CUR);
}
return fseek($fh, gmp_intval($offset), SEEK_CUR);
}
fseek64($f, '23456781232');
对于我的项目,我需要从 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 的请求除外。