computerwiz
04-19-2009, 11:01 PM
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:
<?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:
<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.
<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>
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:
<?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:
<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.
<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>