PHP serving a large file download times out


I'm trying to use PHP to serve up a file download. I can get the file to begin downloading, but it has only ever been able to download about 280MB of a 1GB file. I'm on a shared host so this could be the issue, if it's not an issue with my code:

function downloadFile($file){
    if (file_exists($file)) {
        if(is_dir($file)){return false;}
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Transfer-Encoding: binary');
        header('Content-Disposition: attachment; filename="'.basename($file).'"');
        header('Content-Transfer-Encoding: binary');
        header('Connection: Keep-Alive');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: '.sprintf("%u", filesize($file)));

        $handle = fopen($file, "rb");
        $chunksize=(sprintf("%u", (filesize($file))/1024));

        while (!feof($handle)) {
            echo fgets($handle, $chunksize);
    }else{ echo "file not found";}

People are also looking for solutions to the problem: javascript - Manage json data outside ajaxform


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.