Download Per Day - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Download Per Day

DJ PremDJ Prem Posts: 33Member
I have a file that I would like to share with the public but i would like to limit the number of downloads as i have limited bandwidth.

I can give 200 downloads per day for the file Would it be possible to allocate slots so that per day lets say only 200 downloads can be done once this is done the downloader is give a message saying please come back tomorrow.

I have PHP, MySQL, CGI-BIN support.


Also can I only let one downloader have one connections as many download managed support multiple connections. BUt htis is not the main thing as long as the downloading limit works

Comments

  • emperoremperor Posts: 59Member
    Please correct me if there's a better way (there probably is)...

    First off PHP needs to know when the file is downloaded, so you might want to put it in a directory that is not available to the public and add code into the page, something like this:
    [code]
    <?php
    if (isset ($_GET['download']) and is_file ("directory/$_GET[download]"))
    {
    header ('Content-Type: file');
    header ("Content-Disposition: attachment; filename=$_GET[download]");
    readfile ("directory/$_GET[download]");
    exit;
    }
    ?>



    Download and whatnot



    download me!


    [/code]

    Then you need to count downloads. The best way would be to create a MySQL table with the current date and the total number of downloads for today, like this:

    [code]
    create table downloads (
    today date not null,
    total int(3) not null
    );
    [/code]

    In order to total the downloads per day, you'd then add some code to the above "if" block and do something like this:

    [code]
    <?php
    if (isset ($_GET['download']) and is_file ("directory/$_GET[download]"))
    {
    mysql_connect ('localhost', 'username', 'password');
    mysql_select_db ('database');

    $today = mysql_result (mysql_query ('SELECT CURDATE() = today FROM downloads'), 0);

    if ($today)
    mysql_query ('UPDATE downloads SET total=total+1 WHERE total < 200');
    else
    mysql_query (sprintf ("UPDATE downloads SET today='%s', total=1", date ('Y-m-d')));

    if (mysql_affected_rows ())
    {
    header ('Content-Type: file');
    header ("Content-Disposition: attachment; filename=$_GET[download]");
    readfile ("directory/$_GET[download]");
    exit;
    }
    else
    $error = 'download not available';
    }
    ?>



    Download and whatnot



    <?= $error ?>
    download me!


    [/code]

    Although you'd probably want some better error checking, but that's basically how I'd do it
  • DJ PremDJ Prem Posts: 33Member
    Sounds like what I want to do but i'm quite a newbie to MySQL qould you be kind enough to carry this out for me if i give you the username and password?


Sign In or Register to comment.