Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Aug 2009
    Location
    in .........
    Posts
    263
    Thanks
    183
    Thanked 0 Times in 0 Posts

    How to count the downloads number of a file

    I need to know how to count the downloads number or hits number for a file who uploaded in my site.
    hope if any one can help.

  • #2
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    you can use either a text file if it's just one file, or extend this with a database + table, allowing for more advanced features etc.

    both ways are simple to execute.

  • Users who have thanked met for this post:

    Mido™ (11-03-2009)

  • #3
    Regular Coder
    Join Date
    Aug 2009
    Location
    in .........
    Posts
    263
    Thanks
    183
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by met View Post
    you can use either a text file if it's just one file, or extend this with a database + table, allowing for more advanced features etc.

    both ways are simple to execute.
    no the files will be many,and about the solution of using mysql can you give me just the key or the general view of it?

  • #4
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    i would create a table like this

    PHP Code:
    CREATE TABLE `your_database`.`tblDownloads` (
    `
    file_idINTNOT NULL AUTO_INCREMENT PRIMARY KEY
    `file_nameVARCHAR50 NOT NULL ,
    `
    file_locationVARCHAR50 NOT NULL ,
    `
    upload_dateDATETIME NOT NULL ,
    `
    download_hitsINTNOT NULL DEFAULT '0' ,
    PRIMARY KEY ( `file_id` )
    ENGINE MYISAM 
    every time you add a new file to your library of files do an insert query with the necessary information.

    Set up a downloads page LIKE

    PHP Code:
    $get_downloads mysql_query("SELECT * FROM tblDownloads");
    while(
    $row=mysql_fetch_array($get_downloads)) {
        echo 
    '<a href="download.php?file_id='.$row['file_id'].'">Download '$row['file_name'] . '</a><br />';

    on download.php have something LIKE

    PHP Code:
    if(isset($_GET['file_id'])) {
    $file_id $_GET['file_id']; /* validate this */


    $update=mysql_query("UPDATE `your_database`.`tblDownloads` SET `download_hits` = `download_hits` +1 WHERE `tblDownloads`.`file_id` ="$file_id " LIMIT 1");

    if(
    $update) { 'thanks for downloading...'; } 
    Last edited by met; 11-03-2009 at 08:02 PM.

  • Users who have thanked met for this post:

    Mido™ (11-03-2009)

  • #5
    Regular Coder
    Join Date
    Aug 2009
    Location
    in .........
    Posts
    263
    Thanks
    183
    Thanked 0 Times in 0 Posts
    Thanks much or you help,but I have a problem.
    the files didn't downloaded!
    when I want to download a file on my server I set the attribute "href" of anchor = the file name.
    my download link is look like this:

    Code:
    <a href='files/$row[file_name]?do=hit&num=$row[id]' > $row[file_name] </a>
    and I added the part of "?do=hit&num=$row[id]" after I saw you last post but I the variable of GET didn't set.
    are you have a solution or another way to download the file who in your server?

  • #6
    met
    met is offline
    Regular Coder
    Join Date
    Oct 2009
    Location
    United Kingdom
    Posts
    728
    Thanks
    4
    Thanked 119 Times in 119 Posts
    need more code to see if you did something wrong but the line you provided suggests you haven't done it correctly

    should be something like

    PHP Code:
    echo '<a href="files/'.$row['file_name'].'?do=hit&num='.$row['id'].'">'$row['file_name'] . '</a>'
    also look in to http://php.net/manual/en/function.header.php

    PHP Code:
    <?php
      header
    ("Content-Disposition: attachment; filename=\"" basename($filename) . "\"");
    ?>
    for downloads via php, might be of relevance

  • Users who have thanked met for this post:

    Mido™ (11-04-2009)

  • #7
    Regular Coder
    Join Date
    Aug 2009
    Location
    in .........
    Posts
    263
    Thanks
    183
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by met View Post
    need more code to see if you did something wrong but the line you provided suggests you haven't done it correctly

    should be something like

    PHP Code:
    echo '<a href="files/'.$row['file_name'].'?do=hit&num='.$row['id'].'">'$row['file_name'] . '</a>'

    this didn't worked also,the get array didn't exist.


    Quote Originally Posted by met View Post
    also look in to http://php.net/manual/en/function.header.php

    PHP Code:
    <?php
      header
    ("Content-Disposition: attachment; filename=\"" basename($filename) . "\"");
    ?>
    for downloads via php, might be of relevance
    I see this way is more suitable and thanks for provide it,I tried it and it work well but not by 100%.
    I found 2 problems:
    - the count increase by 3 after downloading the file although I set LIMIT = 1 in the update query.
    - the downloaded files size is 0, with any type.

  • #8
    Regular Coder
    Join Date
    Aug 2009
    Location
    in .........
    Posts
    263
    Thanks
    183
    Thanked 0 Times in 0 Posts
    hope if any one can help me in this serious problem,I tried many ways and it didn't work.

  • #9
    Regular Coder
    Join Date
    Aug 2009
    Location
    in .........
    Posts
    263
    Thanks
    183
    Thanked 0 Times in 0 Posts
    problem solved.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •