Force download large file with php

Many users of my site reported problems downloading a large file (80 MB). I use forced loading using headers. If necessary, I can provide additional php settings. I am using CakePHP framework, but this code is regular php. I am using php 5.2 with apache on a dedicated virtual server from the CentOS Linux media temple. You see any problems with the following code:

        set_time_limit(1500);
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=\"" . basename($file_path) . "\"");
        header("Content-Length: ".$content_length);
        header("Content-Transfer-Encoding: binary");
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Cache-Control: private', false);
        header('Pragma: public');
        header('Expires: 0');

        //Change this part
        $handle = fopen($file_path, 'rb');
        while (!feof($handle))
        {
            echo fread($handle, 4096);
            ob_flush();
            flush();
        }
        fclose($handle);
        exit;

Basically, the problem is that the download starts and then stops in the middle. I thought this was a limit problem, so I am adding the set_time_limit code. I used to use the php reader function, but that also did not work smoothly.

+3
2

PHP HTTP , :

GET /yourfile HTTP/1.1
Range: bytes=31489531-79837582

, , . php script ( , ).

. - . , , , cookie (./allowed/178.224.2.55-file-1). HTTP- .meta.

+4

, S & G set_time_limit while. , ( ) .

+1

All Articles