You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Project that allows you to manipulate huge files in PHP. (currently supports only getting file size)
5
3
6
-
This project was (originally) response for stackoverflow question. http://stackoverflow.com/questions/5501451/php-x86-how-to-get-filesize-of-2gb-file-without-external-program
4
+
This project is collection of hacks that are needed to manipulate files over 2GB in PHP. Currently there is support for getting **exact file size**. This project is originally answer for [stackoverflow question](http://stackoverflow.com/questions/5501451/php-x86-how-to-get-filesize-of-2gb-file-without-external-program).
7
5
8
-
Example:
6
+
Example usage:
7
+
````php
8
+
require "BigFileTools.php";
9
9
10
-
<?php
11
-
12
-
require "BigFileTools.php";
13
-
$f = BigFileTools::fromFile("favoriteFilm.mkv");
14
-
echo $f->getSize()." bytes\n";
15
-
16
-
?>
17
-
18
-
This project uses BCMath or GMP for calculating big numbers. (automatically chosen) So getSize returns string for maximal precision.
Please note, that getSize returns `string`. This is due to fact we cannot be sure that php-integer will be able to store that big value.
20
+
21
+
## Under the hood ##
22
+
23
+
To get insight into what is happening we need a little introduction in how numbers are represented in digital world.
24
+
25
+
The problem lies in the fact that PHP uses 32-bit signed integer on most platforms. PHPx64 for Linux 64-bit version uses 64-bit so you do not need this library there anymore. On the other hand 64-bit version of PHP for 64-bit Windows uses 32-bit integer. Because PHP uses signed integers this means that there is one bit for sign (+-) and rest is used for value.
26
+
27
+
````
28
+
32-bit signed integer max value: +2^31 = 2 147 483 648 ~ 2 gigabytes
29
+
64-bit signed integer max value: +2^63 = 9 223 372 036 854 775 808 ~ 9 223 372 000 gigabytes
30
+
````
31
+
32
+
To overcome this problem this library uses string representation of numbers which means that only you RAM is limit of number size.
33
+
34
+
**Caution:** There are tons of non-solutions for this problem. Most of them looks like `sprintf("%u", filesize($file));`. This does NOT solve problem. If just shifts it a little. The `%u` assumes give value as **unsigned** integer. This means that first signing bit is treated also as a value. Unfortunately this means that boundary was just shifted from 2 GB limit to 4 GB.
35
+
36
+
Second problem is that standard file manipulation APIs fails with strange errors or returns completely weird values. Library therefore implements more of doing things and tries what works on your platform. They are executed from the fastest to the slowest method. On my test data results were:
19
37
20
-
There are several ways how to get proper file size for big files in PHP. (ordered by runtime)
21
38
22
39
method time
23
40
------ ----
@@ -27,9 +44,8 @@ There are several ways how to get proper file size for big files in PHP. (ordere
27
44
sizeExec 0.042937040328979
28
45
sizeNativeRead 2.7670161724091
29
46
30
-
getSize() tries to get size using these method as in order above to be as fast as possible on your platform.
31
47
32
48
Requirements
33
49
------------
34
50
35
-
This class is designed to use with Nette. However it is really simple to remove this dependency. If you want to do that follow first comment in BigFileTools.php.
51
+
If composer does not fail to install, it is safe to use on your system.
0 commit comments