Объем содержимого ограничен 2 ГБ


Я создал скрипт, который позволяет пользователю загружать файлы:

function file_size($filename)
{
    exec('stat -c %s ' . escapeshellarg($filename), $return);
    return (float)$return[0];
}

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($filename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . file_size($filename));

readfile($filename);

exit;

Очень просто. Функция file_size позволяет мне определять размеры файлов, превышающие 2 ГБ.

Проблема заключается в том, что Content-length никогда не превышает 2 ГБ:

< HTTP/1.1 200 OK
< Date: Sun, 21 Aug 2011 09:33:20 GMT
< Server: Apache
< Content-Description: File Transfer
< Content-Disposition: attachment; filename=very-large-file
< Content-Transfer-Encoding: binary
< Expires: 0
< Cache-Control: must-revalidate, post-check=0, pre-check=0
< Pragma: public
< Content-Length: 2147483647
< Content-Type: application/octet-stream

Выполнение var_dump для 'Content-Length: ' . file_size($filename) возвращает string(26) "Content-Length: 4689218232". Если я получу доступ к файлу напрямую без PHP-скрипта, проблем не возникнет, и Apache сообщит правильный размер файла:

< HTTP/1.1 200 OK
< Date: Sun, 21 Aug 2011 09:58:33 GMT
< Server: Apache
< Last-Modified: Thu, 06 Jan 2011 21:56:47 GMT
< ETag: "8ba8f5e0-1177fcab8-49934940b30e5"
< Accept-Ranges: bytes
< Content-Length: 4689218232

Но мне бы очень хотелось передать файл через мой PHP-скрипт. Благодарю тебе за потраченное время.

Author: Znarkus, 2011-08-21

1 answers

Отправка огромных файлов с помощью readfile не является хорошей практикой.

Используйте X-Sendfile, например: header("X-Sendfile: $filename");. Вам понадобится apache mod_sendfile. Это также должно решить вашу проблему с размером файла.

 3
Author: Karoly Horvath, 2011-08-21 10:06:41