PHP x86如何获得>的文件大小2 GB文件没有外部程序? [英] PHP x86 How to get filesize of > 2 GB file without external program?

查看:102
本文介绍了PHP x86如何获得>的文件大小2 GB文件没有外部程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获得大小超过2 GB的文件的文件大小。 (在4.6 GB文件上测试)。

目前的状态:


    ()和 fseek()
  • fread() feof() li>


有可能通过读取文件内容来获得文件大小(非常慢!)。

  $ size =(float)0; 
$ chunksize = 1024 * 1024; ($ fe $($ fp)){
fread($ fp,$ chunksize);
$ size + =(float)$ chunksize;
}
return $ size;

我知道如何在64位平台上使用它(使用 fseek $ fp,0,SEEK_END) ftell()),但是我需要32位平台的解决方案。 b




解决方案:我已经为此启动了开源项目。大文件工具(Big File Tools)大文件工具(Big File Tools)是PHP中(甚至在32位系统上)操作超过2 GB的文件所需的一系列黑客的集合。


解决方案

项目称为大文件工具。这是证明在Linux,Mac和Windows上工作(甚至是32位变体)。即使对于大文件(> 4GB),它也能提供字节精确的结果。它在内部使用 - 任意精度算术库。

使用作曲家进行安装。



<$ p $使用它:

 <?php 
$ file = BigFileTools\BigFileTools :: createDefault() - > getFile(__ FILE__);
echo $ file-> getSize()。 bytes \\\
;

结果是BigInteger,所以您可以计算结果

  $ sizeInBytes = $ file-> getSize(); 
$ sizeInMegabytes = $ sizeInBytes-> toBigDecimal() - > dividedBy(1024 * 1024,2,\\ \\ Brick \Math \RoundingMode :: HALF_DOWN);
echoSize is $ sizeInMegabytes megabytes \\\
;大型文件工具在内部使用驱动程序来可靠地确定确切数据所有平台上的文件大小。这里是可用的驱动程序列表(更新2016-02-05)

  |驱动程序|时间(秒)↓|运行时需求|平台
| --------------- | ------------------- | -------------- | ---------
| CurlDriver | 0.00045299530029297 | CURL扩展| -
| NativeSeekDriver | 0.00052094459533691 | - | -
| ComDriver | 0.0031449794769287 | COM + .NET扩展| Windows只有
| ExecDriver | 0.042937040328979 | exec()启用| Windows,Linux,OS X
| NativeRead | 2.7670161724091 | - | -

您可以使用BigFileTools, c> BigFileTools :: createDefault()

 使用BigFileTools \ BigFileTools; 
使用BigFileTools \Driver;
$ bigFileTools = new BigFileTools(new Driver \CurlDriver());


I need to get the file size of a file over 2 GB in size. (testing on 4.6 GB file). Is there any way to do this without an external program?

Current status:

  • filesize(), stat() and fseek() fails
  • fread() and feof() works

There is a possibility to get the file size by reading the file content (extremely slow!).

$size = (float) 0;
$chunksize = 1024 * 1024;
while (!feof($fp)) {
    fread($fp, $chunksize);
    $size += (float) $chunksize;
}
return $size;

I know how to get it on 64-bit platforms (using fseek($fp, 0, SEEK_END) and ftell()), but I need solution for 32-bit platform.


Solution: I've started open-source project for this.

Big File Tools

Big File Tools is a collection of hacks that are needed to manipulate files over 2 GB in PHP (even on 32-bit systems).

解决方案

I've started project called Big File Tools. It is proven to work on Linux, Mac and Windows (even 32-bit variants). It provides byte-precise results even for huge files (>4GB). Internally it uses brick/math - arbitrary-precision arithmetic library.

Install it using composer.

composer install jkuchar/BigFileTools

and use it:

<?php
$file = BigFileTools\BigFileTools::createDefault()->getFile(__FILE__);
echo $file->getSize() . " bytes\n";

Result is BigInteger so you can compute with results

$sizeInBytes = $file->getSize();
$sizeInMegabytes = $sizeInBytes->toBigDecimal()->dividedBy(1024*1024, 2, \Brick\Math\RoundingMode::HALF_DOWN);    
echo "Size is $sizeInMegabytes megabytes\n";


Big File Tools internally uses drivers to reliably determine exact file size on all platforms. Here is list of available drivers (updated 2016-02-05)

| Driver           | Time (s) ↓          | Runtime requirements | Platform 
| ---------------  | ------------------- | --------------       | ---------
| CurlDriver       | 0.00045299530029297 | CURL extension       | -
| NativeSeekDriver | 0.00052094459533691 | -                    | -
| ComDriver        | 0.0031449794769287  | COM+.NET extension   | Windows only
| ExecDriver       | 0.042937040328979   | exec() enabled       | Windows, Linux, OS X
| NativeRead       | 2.7670161724091     | -                    | -

You can use BigFileTools with any of these or fastest available is chosen by default (BigFileTools::createDefault())

 use BigFileTools\BigFileTools;
 use BigFileTools\Driver;
 $bigFileTools = new BigFileTools(new Driver\CurlDriver());

这篇关于PHP x86如何获得&gt;的文件大小2 GB文件没有外部程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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