I am working with large files in PHP, and need a RELIABLE way to get the file size of larger files over 4 GB, but PHP runs into problems with files over 2 GB... so far I have only seen solutions involving command line exec
features, but the script will be used as a standalone console application, therefore, I am a bit hesitant to using exec
as it might react differently on different platforms. The only way left as I see it is reading all the data and count the bytes, but this would be VERY slow... I need a fast and reliable way that will react equally on many different computers (Linux, Windows, Mac).
Asked
Active
Viewed 5,198 times
1

David Ferenczy Rogožan
- 23,966
- 9
- 79
- 68

Daniel
- 319
- 8
- 17
-
232-bit PHP has problems with files >2GB, 64-bit PHP can handle a lot larger files – Mark Baker May 31 '13 at 22:01
-
I don't know any solution other than exec'ing a ls command for example. PHP's integer is signed, so you won't be able to do it with PHP's internal functions (on 32 bit systems) – bwoebi May 31 '13 at 22:01
-
Similar question? http://stackoverflow.com/questions/5501451/php-x86-how-to-get-filesize-of-2gb-file-without-external-program?rq=1 – wazy May 31 '13 at 22:04
-
can php x64 run on x32 systems? and where can i get it? :) – Daniel May 31 '13 at 22:04
-
If you're running on a 32-bit system, then I wouldn't expect files that can exceed the 2GB OS limitation – Mark Baker May 31 '13 at 22:05
-
No i am not, but as i mentioned the script will function as a command line app, and some people that will run it might do so on a 32bit system... – Daniel May 31 '13 at 22:06
-
64-bit PHP 5.5 for Windows binaries available here: http://windows.php.net/qa/ – Mark Baker May 31 '13 at 22:06
-
1https://github.com/jkuchar/BigFileTools from http://stackoverflow.com/questions/5501451/php-x86-how-to-get-filesize-of-2gb-file-without-external-program?rq=1 – wazy May 31 '13 at 22:07
-
If they're running it locally, they'll be working with the version of PHP that they have installed already, surely - or are you providing PHP as well as the script? – Mark Baker May 31 '13 at 22:12
-
If you check [this file](https://github.com/jkuchar/BigFileTools/blob/master/class/BigFileTools.php) from @wazy's link you will see several options for getting the size of a file larger than 2GB. Or you can just use that script. And no, 64-bit anything will never work on a 32-bit system. – Sammitch May 31 '13 at 22:17
2 Answers
2
This previously asked question seems very similar and has some ideas in it that you could explore: PHP x86 How to get filesize of > 2 GB file without external program?
In it the author comes up with a solution that he hosts on GitHub, the link is located here: https://github.com/jkuchar/BigFileTools/blob/master/src/BigFileTools.php
Beyond that you are running a 32 bit system and thus files over 2 GB will be troublesome in PHP from http://php.net/manual/en/function.filesize.php:
Note: Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB.
-
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/11165337) – Jeff Feb 06 '16 at 13:13
-
@Jeff I don't see how this doesn't answer the question? BigFileTools states on the GitHub page: "This project is collection of hacks that are needed to manipulate files over 2GB in PHP (even on 32-bit systems)." – wazy Feb 09 '16 at 00:56
-2
Below code works OK for any filesize on any version of 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

PeeHaa
- 71,436
- 58
- 190
- 262

Tech Consultant
- 374
- 1
- 7