Skip to content

Commit 8b4a579

Browse files
committed
docs: Updated README.md with more explanation of how things work
1 parent df9e05e commit 8b4a579

File tree

2 files changed

+36
-19
lines changed

2 files changed

+36
-19
lines changed

README.md

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
1-
BigFileTools
2-
============
1+
# BigFileTools #
32

4-
Project that allows you to manipulate huge files in PHP. (currently supports only getting file size)
53

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).
75

8-
Example:
6+
Example usage:
7+
````php
8+
require "BigFileTools.php";
99

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.
10+
$size = BigFileTools::fromPath(__FILE__)->getSize();
11+
var_dump($size);
12+
echo "Example files size is " . $size . " bytes\n";
13+
````
14+
Will produce output:
15+
````
16+
string(3) "176"
17+
Example files size is 176 bytes
18+
````
19+
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:
1937

20-
There are several ways how to get proper file size for big files in PHP. (ordered by runtime)
2138

2239
method time
2340
------ ----
@@ -27,9 +44,8 @@ There are several ways how to get proper file size for big files in PHP. (ordere
2744
sizeExec 0.042937040328979
2845
sizeNativeRead 2.7670161724091
2946

30-
getSize() tries to get size using these method as in order above to be as fast as possible on your platform.
3147

3248
Requirements
3349
------------
3450

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.

examples/example1.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22

33
require '../class/BigFileTools.php';
44

5-
6-
echo $f = BigFileTools::fromPath(__FILE__)->getSize()." bytes";
5+
$size = BigFileTools::fromPath(__FILE__)->getSize();
6+
var_dump($size);
7+
echo "Example files size is " . $size . " bytes\n";

0 commit comments

Comments
 (0)