PHP: How to know if a user is currently downloading a file?

Posted by metrobalderas on Stack Overflow See other posts from Stack Overflow or by metrobalderas
Published on 2011-01-07T01:44:53Z Indexed on 2011/01/10 0:53 UTC
Read the original article Hit count: 132

Filed under:
|
|

I'm developing a quick rapidshare-like site where the user can download files. First, I created a quick test setting headers and using readfile() but then I found in the comments section there's a way to limit the speed of the download, which is great, here's the code:

$local_file = 'file.zip';
$download_file = 'name.zip';

// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;
if(file_exists($local_file) && is_file($local_file))
{
    header('Cache-control: private');
    header('Content-Type: application/octet-stream');
    header('Content-Length: '.filesize($local_file));
    header('Content-Disposition: filename='.$download_file);

    flush();
    $file = fopen($local_file, "r");
    while(!feof($file))
    {
        // send the current file part to the browser
        print fread($file, round($download_rate * 1024));
        // flush the content to the browser
        flush();
        // sleep one second
        sleep(1);
    }
    fclose($file);}
else {
    die('Error: The file '.$local_file.' does not exist!');
}

But now my question is, how to limit the number of downloads at the same time? How can I check there's still a connection with some user's IP?

Thanks.

© Stack Overflow or respective owner

Related posts about php

Related posts about download