PHP: how do I know if a user is uploading a file?

I am developing a fast website similar to rapidshare, where the user can upload files. Firstly, I created quick headers for the test parameters and used readfile(), but then I found in the comments section to limit the download speed, which is great, here is 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 a time? How can I check if there is still a connection to some user IP?

Thank.

+3
source share
1 answer

Does the user have a login? if you donโ€™t just use sessions or even better track their ip address.

Here is an example session:

$_SESSION['file_downloading']==true;
$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);
}
$_SESSION['file_downloading']=null;
fclose($file);}

Then, first of all, this code,

if(!empty($_SESSION['file_downloading'])) 

// - .

- ip-.

//http://wiki.jumba.com.au/wiki/PHP_Get_user_IP_Address
function VisitorIP()
    { 
    if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
        $TheIp=$_SERVER['HTTP_X_FORWARDED_FOR'];
    else $TheIp=$_SERVER['REMOTE_ADDR'];

    return trim($TheIp);
    }

IP- , . ip-, . ?

+3

Source: https://habr.com/ru/post/1783989/


All Articles