Results 1 to 5 of 5

Thread: Download Counter, a little problem ?

  1. #1
    Join Date
    Mar 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Download Counter, a little problem ?

    I found a simple script but have one problem with it, if someone would look at it for me.

    Basically everything works fine but on the page that users see it does not show the xx times file was downloaded. I think it is a simple error or something, if someone could tell me what it is so i can sort it please.

    here is everything for the script:
    ==================

    Download counter script using PHP. It simply counts each click on the download link and stores the result in a writable text file.

    First upload the file you wish users to download to your server. Then create a text file and name it “counter.txt“. Open the text file and add a 0, save and upload to your server and set the text file permissions to 777.

    Now create a PHP file named “countdownloads.php” and add the following code:

    Code:
        <?php
        $myFile = "counter.txt";
        $fh = fopen($myFile, 'r');
        $theData = fread($fh, filesize($myFile));
        fclose($fh);
    
        $theData = $theData + 1;
    
        $myFile = "counter.txt";
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $theData);
        fclose($fh);
    
        header("Location: download.zip");
        ?>
    Now add the following code to your HTML page that will display the link to your download:

    Code:
        <p><a href="countdownloads.php">Download File</a><br />
        <?php
        $myFile = "counter.txt";
        $fh = fopen($myFile, 'r');
        $theData = fread($fh, filesize($myFile));
        echo $theData;
        fclose($fh);
        ?> Downloads<br />
        since Feb 2008</p>
    Upload these files to the same directory on your server and that should do the trick.

    The part that i think would be wrong is this bit of code,

    I think it has something to do with this bit of code that outputs xx downloads since Feb 2009. All is working great etc, downloads are counted like they should in the counter.txt file the download link and 'downloads since feb 2009' shows on page but not the amount the file has been downloaded.

    Code:
    <p><a href="countdownloads.php">Download File</a><br />
        <?php
        $myFile = "counter.txt";
        $fh = fopen($myFile, 'r');
        $theData = fread($fh, filesize($myFile));
        echo $theData;
        fclose($fh);
        ?> Downloads<br />
        since Feb 2008</p>

  2. #2
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    I put 25 in the counter.txt file and uploaded it, then tested the code where it would display the amount and it worked ok.

    25 Downloads
    since Feb 2008

    I didn't have to change any permissions for counter.txt but have you given that a try yet? Within your FTP program you might try to CHMOD the counter.txt file to 777 which would give all permissions. That may not be your problem, but it would be something quick to check if you haven't already to see if it provides a solution.

  3. #3
    Join Date
    Mar 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi,

    I did check file permissions, my server does not support 777 for security reasons. Instead i tried 644, 666, 755 and still nothing.

    I don't don't why it won't show. Everything seems to work perfectly apart from it won't show xx number of times file been downloaded.

    I have double checked everything over and over and still nothing.

    I am baffeled on what the problem is.

    Thanks
    computerwiz

  4. #4
    Join Date
    Mar 2009
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Hi,

    I manged to figure it out, it must be my host as i tried with another host and it worked.

    I have contacted my host but problem is they disabled CHMOD 777 for security purposes, i have tried 644, 666, 755 but still nothing, not sure what other permission i could try.

    As it dont work on my host unless someone could suggest another CHMOD permission i could try my only option is seeing if someone would alter it so it uses a MySQL database so it won't require a file permission to access counter.txt file, instead it would get it from the database. something along those lines.

    Any ideas on another CHMOD permission i could try?

    Thanks
    computerwiz

  5. #5
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    BTW welcome to the forums.

    At least you narrowed it down and know that it isn't anything with the script you were trying to use but rather the permissions on the counter file. I think you have tried pretty much all you can as far as permissions go. I haven't tested it, but I did a quick search and found something that looks pretty simple and uses a database.

    PHP Code:
    <?php 
    /* 
      * click_count.php plebian.com 24/02/02 
      * keeps track of the hits to your links that go through this file. 
      * 
      * create the below table in mysql 

            CREATE TABLE TW_click_count (id int(255) unsigned NOT NULL auto_increment,  url varchar(255) default NULL, hits int(255) unsigned default 1,  PRIMARY KEY  (id),  KEY id (id),   KEY url (url), KEY(hits)) TYPE=MyISAM; 

       * enter the correct mysql username, password and database name as per lines 25 and 26. 
       * 
       * adding a new url to the list is automatic, if the url doesn't exist in the database, it'll be added
       *
       * when making your links, create the link such as, THIS_FILE_NAME.php?j=HTTP://URL.COM/HERE/
       * when that link is clicked, it'll add the link to the database and count all future clicks
       *
       * ie: <a href=count.php?j=http://plebian.com>plebian.com</a>
       *
       * to view the number of hits, you'll need to browse the table using something like phpMyAdmin
       */

    if(!empty($HTTP_GET_VARS['j'])) 

        
    /* enter your mysql username and password used to connect to database_name */ 
        
    $a=mysql_connect("localhost""user""password"); 
        
    $b=mysql_select_db("database_name",$a);
        
        
    $j=addslashes($HTTP_GET_VARS['j']); 
        
    $c=mysql_query("select id from TW_click_count where url='$j' order by id desc limit 1"); 
        
    $d=mysql_fetch_object($c); 
        if(
    is_object($d)) 
        { 
            
    mysql_query("update TW_click_count set hits=hits+1 where id={$d->id}"); 
        } 
        else
        {
            
    mysql_query("insert into TW_click_count (url)values('$j')");
        }
        
    header("Location: $j");
    }
    else
    {
        
    header("Location: http://{$HTTP_SERVER_VARS['SERVER_NAME']}");
    }
    ?>
    You might be able to change select id from to select * from that way it pulls all the fields then you could try to echo out on the page the amount in the hits column.

    You might also be able to do some type of download click counter in javascript.

    Hope that helps.

Bookmarks

Posting Permissions

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