how to create a download counter

Discussion in 'PHP' started by vaakash, Nov 7, 2008.

  1. #1
    Hello friends,

    I am having an app for downloading. But i want to know how many of them are downloading.

    How to create a download counter in PHP or any other script. :confused:
     
    vaakash, Nov 7, 2008 IP
  2. Nick_Mayhem

    Nick_Mayhem Notable Member

    Messages:
    3,486
    Likes Received:
    338
    Best Answers:
    0
    Trophy Points:
    290
    #2
    Create a table or just a file on your server which has write permissions.

    On the link where user downloads the file. Just read the counter from file or table and update counter with counter = counter + 1.

    Use some meta refresh tag after that update line to redirect user for downloading the app.

    Thanks.
     
    Nick_Mayhem, Nov 7, 2008 IP
  3. vaakash

    vaakash Member

    Messages:
    127
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    35
    #3
    Sorry Nick_Mayhem, i am not an expert in PHP. Please give a tutorial on your above post, if you have time.

    Thanks for your patience.
     
    vaakash, Nov 7, 2008 IP
  4. CreatorKit.com

    CreatorKit.com Active Member

    Messages:
    581
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    60
    #4
    if you have a MySQL DB with name BASE1, table TABLE1 and with 2 fields DOWNLOADS and APP_ID then

    $db = mysql_connect("MySQL SERVER HERE or localhost as default", "MySQL LOGIN HERE", "MySQL PASSWORD HERE");
    
    $APP_ID = $_GET['id'];
    
    mysql_select_db("BASE1",$db);
    	
    $result = mysql_query("SELECT * FROM TABLE1 WHERE APP_ID='$APP_ID'",$db);
    $result = mysql_fetch_array($result);
    
    {
    	$downloads = $result["DOWNLOADS"];
    	$downloads++;
    	mysql_query("UPDATE TABLE1 SET DOWNLOADS='$downloads' WHERE APP_ID=$APP_ID");
    }
    
    mysql_close($db);
    PHP:
    try to do it this way =)
    if you are totally newb in PHP, then try to read some PHP tutorials...

    cheers
     
    CreatorKit.com, Nov 8, 2008 IP
  5. szkoda

    szkoda Banned

    Messages:
    189
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    28
    #5
    Or you could use a pre-made script which does it all for you. I use ccount, just google it, works great for me.
     
    szkoda, Nov 8, 2008 IP
  6. snvc

    snvc Peon

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Here's a simple download counts without DB.

    Save the following as anyfilename.php
    <?php
    
    $fp = fopen("dlcounts.txt", "r");
    if (!$fp) die("Cannot open file(r)");
    $number = fread($fp, filesize("dlcounts.txt"));
    
    $number++;
    
    fclose($fp);
    $fp = fopen("dlcounts.txt", "w");
    if (!$fp) die("Cannot open file(w)");
    fwrite($fp, $number);
    fclose($fp);
    //Place you file here
    $cm = "Location:yourfilename.zip $file";
    header ($cm);
    
    ?>
    PHP:
    Create a text file named dlcounts.txt and chmod permission to 777.

    Place them in the same directory.

    Access the download at anyfilename.php

    Get the download counts by echo the following code:
    <?php
    $fp = fopen("dlcounts.txt", "r");
    echo fread($fp, filesize("dlcounts.txt"));
    fclose($fp);
    ?>
    PHP:
     
    snvc, Nov 8, 2008 IP