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

Wiggler Jtag
  • 669
  • 8
  • 23

1 Answers1

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

Walter Tross
  • 12,237
  • 2
  • 40
  • 64
  • 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 09 '13 at 13:24
  • Tthanks for your reply. I think I ended with filesize($_FILES["file"]["tmp_name"]); this works fine. – Wiggler Jtag Jul 09 '13 at 13:40