Downloading large file with php

Posted by Alessandro on Stack Overflow See other posts from Stack Overflow or by Alessandro
Published on 2010-12-28T18:36:51Z Indexed on 2010/12/28 18:53 UTC
Read the original article Hit count: 213

Filed under:

Hi,

I have to write a php script to download potentially large files. The file I'm reporting here works fine most of the times. However, if the client's connection is slow the request ends (with status code 200) in the middle of the downloading, but not always at the very same point, and not at the very same time. I tried to overwrite some php.ini variables (see the first statements) but the problem remains. I don't know if it's relevant but my hosting server is SiteGround, and for simple static file requests, the download works fine also with slow connections.

I've found Forced downloading large file with php but I didn't understand mario's answer. I'm new to web programming.

So here's my code.

    <?php
    ini_set('memory_limit','16M');
    ini_set('post_max_size', '30M');
    set_time_limit(0);

    include ('../private/database_connection.php');


    $downloadFolder = '../download/';
    $fileName = $_POST['file'];
    $filePath = $downloadFolder . $fileName;
    if($fileName == NULL)
    {
        exit;
    }
    ob_start();
    session_start();


    if(!isset($_SESSION['Username']))
    {
        // or redirect to login (remembering this download request)
        $_SESSION['previousPage'] = 'download.php?file=' . $fileName;
        header("Location: login.php");
        exit;
    }

    if (file_exists($filePath))
    {               
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        //header('Content-Disposition: attachment; filename='.$fileName);
        header("Content-Disposition: attachment; filename=\"$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: ' . filesize($filePath));
        ob_clean();
        flush();

        // download
        // 1
//      readfile($filePath);
        // 2
        $file = @fopen($filePath,"rb");
        if ($file) {
          while(!feof($file)) {
            print(fread($file, 1024*8));
            flush();
            if (connection_status()!=0) {
              @fclose($file);
              die();
            }
          }
          @fclose($file);
        }
        exit;
    }
    else
    {
        header('HTTP/1.1 404 File not found');
        exit;
    }
?>

© Stack Overflow or respective owner

Related posts about php