How to upload files larger than 2 GB in PHP?

I am trying to write a script in PHP that downloads a large zip file (2,663,439,370 bytes), and I encountered an interesting but unpleasant problem: the script loads the first 2,147,483,647 bytes, and then continues to load the file, but instead of adding byte number 2.147 to it. 483.648, 2.147.483.649, etc. he continues to add bytes to the file, starting at byte number 1.

Thus, the downloaded file is created from: byte 1, byte 2, ... byte 2.147.483.647, byte 1, byte 2 ... and so on.

I notice that 2.147.483.647 is the maximum integer value that a 32-byte system can store. However, my server is a 64-byte system and can store values ​​that exceed this. To prove this, var_dump ((int) 2147483648) returns the correct integer.

My script loading is as correct as possible (taken from php.net using copy-paste)

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="Certificat.zip"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($zipname));    

readfile($zipname);

Has anyone encountered this problem?

+4
source share
3 answers

In the end, I fixed the problem by excluding the function readfile()and the output of the file itself using fopen()and fread().

However, the reason readfile()'fails to work with such large files remains an open subject (I read the documentation on php.net and I could not find any notes about such a problem). So I'm still waiting for an experienced programmer to enlighten me :).

0
source

Reading source 5.6, readfilereturns RETURN_LONG(size). Here is the source link .

, , int, 4 , , . , , , , ( - ).

, readfile, fopen/fread . , , 2 , .

+1

, , header('Content-Length: ' . filesize($zipname)); , , , script . , , , php.

0

Source: https://habr.com/ru/post/1661591/


All Articles