4

I am getting at >2 GB file upload negative number at $_FILES["file"]["size"].

However file uploads fine and filesize() also returns correct size. But how to solve $_FILES to return correct value?

I have read about the issues at some old versions of php, they also had an error at returning negative number at filesize(), but this seems fine now. Did they just forget to fix $_FILES? Dont you know if its fixed on php 5.5.0 ?

PHP: 5.4.16 OS: Debian Squeeze 6.0.7 x64 Webserver: Nginx 1.2.7

5
  • 1
    What does sprintf('%u', $_FILES['file']['size']) give?
    – Ja͢ck
    Jul 9, 2013 at 13:12
  • 3
    See bugs.php.net/bug.php?id=44522
    – Maerlyn
    Jul 9, 2013 at 13:19
  • Will recommend you using FTP functions. Jul 9, 2013 at 13:21
  • Jack: 18446744072347431725, Maerlyn: I've seen this bug at php.net, but Submitted 2008 and modified in 2012, few patches were done to 5.2 php and then nothing new till today? :/ Jul 9, 2013 at 13:38
  • Atul, yes I know, thanks, will keep in mind :) Jul 9, 2013 at 13:39

1 Answer 1

5

whatever the problem is, you can probably work around it up to 4GB like this:

$file_size = $_FILES["file"]["size"];
$true_size = $file_size >= 0 ? $file_size : 4*1024*1024*1024 + $file_size;

But, as you said, filesize($_FILES["file"]["tmp_name"]); is the safest way to go (will work above 4GB too).

2
  • 4
    Indeed. This bug is due to the size being stored in a signed 32 bits integer. The largest storable value is 2^31 (32 bits minus the sign bit). When the value is overflown the sign bit gets set and the size gets negative. The trick given by Walter emulates the correct behavior by exploiting the int's sign, and it is forward compatible.
    – thibauts
    Jul 9, 2013 at 13:24
  • Tthanks for your reply. I think I ended with filesize($_FILES["file"]["tmp_name"]); this works fine. Jul 9, 2013 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.