1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to check the completed downloads - PHP

Discussion in 'PHP' started by figooo, May 17, 2014.

  1. #1
    Hello,

    if you need to check the completed downloads from your website. you can use the following code

    
            $download_rate = 500; //Download rate 500Kb/sec
            $downloaded_bytes=0;
    
            header('Cache-control: private');
            header('Content-Type: application/octet-stream');
            header('Content-Length: '.filesize($file));
            header('Content-Disposition: filename='.$filename);
    
            flush();
            $file = fopen($file, "r");
    
            while(!feof($file))
            {
                // send the current file part to the browser
                $buf = fread($file, round($download_rate * 1024));
                print $buf;
                $downloaded_bytes += strlen($buf);
                // flush the content to the browser
                flush();
                // sleep one second
                sleep(1);
            }
            fclose($file);
    if($downloaded_bytes==filesize($file))
    {
    //Do any thing like update your database or any thing else
    }
    
    Code (markup):
     
    figooo, May 17, 2014 IP
  2. coolpavan

    coolpavan Well-Known Member

    Messages:
    845
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    120
    #2
    can we manage a counter to count the number of sucessfull downloads ?
     
    coolpavan, May 18, 2014 IP
  3. figooo

    figooo Active Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #3
    sure you can. you can do this by inserting every successful download into the database to make your report about the count of successful downloads.

    example:
    $download_rate = 500; //Download rate 500Kb/sec
            $downloaded_bytes=0;
            header('Cache-control: private');
            header('Content-Type: application/octet-stream');
            header('Content-Length: '.filesize($file));
            header('Content-Disposition: filename='.$filename);
            flush();
            $file = fopen($file, "r");
            while(!feof($file))
            {
                // send the current file part to the browser
                $buf = fread($file, round($download_rate * 1024));
                print $buf;
                $downloaded_bytes += strlen($buf);
                // flush the content to the browser
                flush();
                // sleep one second
                sleep(1);
            }
            fclose($file);
    if($downloaded_bytes==filesize($file))
    {
    /*
    Here you can insert into the database the downloaded size ($downloaded_bytes) of each file and each one row means one successful download
    NOTE: you may need to connect again to database before insert query as the database connection may lost for a large file as it takes more time to download so make sure to open connection before insert query.
    */
    }
    
    /*
    Then you can make another file to run report by selecting the count of rows to get the number of successful downloads
    */
    
    Code (markup):
     
    figooo, May 18, 2014 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    This accomplishes not a whole lot, to be fair. This sends the entire file to the browser, but it doesn't guarantee the user received all of it, or even clicked cancel before receiving everything. The additional check at the bottom is pointless, because at the end of the loop, the whole file will have been sent. PHP won't exit the loop in the middle or jump right out of it. You're probably better off checking connection_status() inside the loop, but that's not entirely fail-safe either.

    On a side note, I would calculate the download rate outside the loop. You're literally calling rand() and doing math thousands of times during a download.
     
    nico_swd, May 20, 2014 IP
  5. figooo

    figooo Active Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #5
    Thanks for your comment. but the example before not to count the downloaded bytes even if the download not completed. this code just tell you when the download is completed but if not cannot tell you the downloaded bytes. if you have another code discussing your idea please post it ;)
     
    figooo, May 21, 2014 IP