私はPHPで大きなファイルを扱っており、4 GBを超える大きなファイルのファイルサイズを取得するための信頼できる方法が必要ですが、PHPは2 GBを超えるファイルで問題に遭遇します...これまでのところ、コマンドラインexec
機能を含むソリューションしか見たことがありませんですが、スクリプトはスタンドアロンのコンソール アプリケーションとして使用されるため、exec
プラットフォームによって反応が異なる可能性があるため、使用するのを少し躊躇しています。私が見る唯一の方法は、すべてのデータを読み取ってバイトをカウントすることですが、これは非常に遅くなります...多くの異なるコンピューター(Linux、Windows、Mac)で等しく反応する、高速で信頼性の高い方法が必要です。
4925 次
2 に答える
-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 に答える