1

我正在处理 PHP 中的大文件,需要一种可靠的方法来获取超过 4 GB 的大文件的文件大小,但是 PHP 遇到超过 2 GB 的文件的问题......到目前为止,我只看到涉及命令行exec功能的解决方案,但该脚本将用作独立的控制台应用程序,因此,我对使用有点犹豫,exec因为它在不同平台上的反应可能不同。我看到的唯一方法是读取所有数据并计算字节数,但这会非常慢......我需要一种快速可靠的方法,可以在许多不同的计算机(Linux、Windows、Mac)上做出同样的反应。

4

2 回答 2

2

这个先前提出的问题似乎非常相似,并且您可以探索一些想法: PHP x86 How to get filesize of > 2 GB file without external program?

在其中,作者提出了他在 GitHub 上托管的解决方案,链接位于此处:https ://github.com/jkuchar/BigFileTools/blob/master/src/BigFileTools.php

除此之外,您正在运行 32 位系统,因此在http://php.net/manual/en/function.filesize.php的 PHP 中超过 2 GB 的文件会很麻烦:

注意:由于 PHP 的整数类型是有符号的,并且许多平台使用 32 位整数,因此对于大于 2GB 的文件,某些文件系统函数可能会返回意外结果。

于 2013-06-01T00:11:55.203 回答
-2

以下代码适用于任何版本的 PHP / OS / Webserver / Platform 上的任何文件大小。

// http head request to local file to get file size
$opts = array('http'=>array('method'=>'HEAD'));
$context = stream_context_create($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
// SECURITY - you must add a .htaccess rule which denies all requests for this database file except those coming from local ip 127.0.0.1.
// $tmp will contain 0 bytes, since its a HEAD request only, so no data actually downloaded, we only want file size
$tmp= file_get_contents('http://127.0.0.1/pages-articles.xml.bz2', false, $context);

$tmp=$http_response_header;
foreach($tmp as $rcd) if( stripos(trim($rcd),"Content-Length:")===0 )  $size= floatval(trim(str_ireplace("Content-Length:","",$rcd)));
echo "File size = $size bytes";

// example output .... 9 GB local file
File size = 10082006833 bytes
于 2013-08-17T11:26:08.030 回答