snippet library logo
in category
PHP / Counters / File Download Counter
Snippet details
ID: 537
Viewed: 1099
Added: 2007-01-31
Version: 1.0
Sorry no demo

User Rated at: 5 Stars
Rate This:
Snippets in this catagory         
Show Printable Version
Simple script for anyone that needs to count how many times a certain file is downloaded, and don't have access to a MySQL database. This first page is where the link to the file will be. You can use javascript to make it a pop up, or as it is here, just a normal link to a page containing the download script.

General Details
Snippet uploaded by: M2EDesigns
Email : admin@m2edesigns.com
Snippet By: M2EDesigns

<!---Head--->
<?php
/*
 *
 *  Script Name: File Download Count Script
 *  File Name: index.php
 *
 *  Code By: Motion 2 Emotion Designs Inc.
 *  Website: http://www.m2edesigns.com
 *
 *  Description: Perfect for anyone that needs to count how many
 *  times a certain file is downloaded, and don't have access to
 *  a MySQL database. This first page is where the link to the
 *  file will be. You can use javascript to make it a pop up, 
 *  or as it is here, just a normal link to a page containing
 *  the download script.
 *
 *  See It In Action At: http://www.stoneddevilinc.com/?i=tm_home.php
 *
 */
?>
<html>
<head>
<title>Download Count Test</title>
</head>
<body>
<h1>Download Script Test</h1>
<?php 
/*
   This is the first part, that is an HTML link to the download 
   script, named download.php. The d_id=0, tells the script which 
   file to download, and which file to increase in the count.
*/ 
?>
<a href="download.php?d_id=0">Click Here To Download</a>
<br>
<?php

/* 
   This is where the file that stores the count is loaded. 
   Change the name "count.txt" to a file the script can access 
     and write to on your server. 
*/
$s_entry file_get_contents("count.txt");

/* 
   This part of the script decodes the data from the flat file
   and puts it in a new array called $the_count.
*/
$the_count unserialize($s_entry);

/*
   This part of the script displays the count for each item. It
     can be put where needed as long as it comes after the 
     unserialize function. Just change the index of the array for
     each different file.
*/
echo 'Downloaded: ' $the_count[0] . ' times';

?>
</body>
</html>

<!---Body--->


&lt;?php
/*
 *
 *  Script Name: File Download Count Script
 *  File Name: download.php
 *
 *  Code By: Motion 2 Emotion Designs Inc.
 *  Website: http://www.m2edesigns.com
 *
 *  Description: This is the file that does most of the work. This is 
 *  what the other file links to. Read below to see what each part does.
 *
 *  See It In Action At: http://www.stoneddevilinc.com/?i=tm_home.php
 *
 */
?&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Download Count Script&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;&lt;h1&gt;Download Will Begin Shortly&lt;/h1&gt;&lt;/center&gt;&lt;br&gt;&lt;br&gt;
&lt;?php 

/* 
   This is where you set the diffrent file names for the download script.
   Just change the array, and add what you will. Remeber, whatever index
   the download file link is in the array, is what you need to set in the
   file that has the links to download 
*/
$f_id= array('test.txt');

/* 
   Next we set the variables needed for the script to run. The first sets
     $the_count as an array, and gets the download id sent through the link
     and sets it to $d_id here in the download count script.
*/
$the_count= array(0);
$d_id = $_GET['d_id'];

/*
   Here we get the contents from the flat file, and decode them so the 
     script can use them. You need to change &quot;count.txt&quot; to the name of 
     the writeable file you are using for this script to work.
*/
$s_entry = file_get_contents(&quot;count.txt&quot;);
$the_count = unserialize($s_entry);

/*
   Here is where we actually increase the count of the download. We
     += 1 to $the_count array, and the $d_id variable tells the array
     which one to increase the count on. 
*/
$the_count[$d_id] += 1;

/*
   Now we open the file that we store the count in so we can write 
     the updated count in it. Remeber to change this to the name of 
     the file you are using for this script!!
*/
$file = fopen(&quot;count.txt&quot;,&quot;w+&quot;);

/*
   Here we serialize $the_count to make the array storable in a flat
     text file. We then write it to the file, and close the file.
*/
$f_entry = serialize($the_count);
fwrite($file,$f_entry);
fclose($file);

/*
   This would be pointless if the user doesn't actually get to 
     download the file. We first set the url to the $f_id array with 
     the $d_id telling us what file in the array to get. next we use a 
     meta refresh to reload the page to the file they need to download.
     We also give them a link to click just in case the download doesn't 
     sstart on its own for some reason.
*/
$url = $f_id[$d_id];
echo '&lt;meta http-equiv=&quot;refresh&quot; content=&quot;5;url='.$url.'&quot;&gt;';
echo 'If Download Does Not Start, &lt;a href='.$url.'&gt;Click Here&lt;/a&gt;';

?&gt;
&lt;/body&gt;
&lt;/html&gt;




No Reviews to show


Please completely fill out the form below if you want to review this snippet. All reviews are subject to validation.


Subject:

Reviewed By:

Write a review:





Terms of Conditions
Powered By
Avian Hosting
© 2005 snippetlibrary.com All Rights Reserved.