Hallo php user
I'm absolute beginner looking for help after days of search.
On my homepage I have a .pdf file that can be downloaded. I want to display a counter next to the file name that shows how many downloads have been made.
On the internet I found many possible scripts, but all of them look very complicated.
Where do I find an easy to understand script with some tutorials?
Thanks for helping me
download counter script
There are probably better ways to do this, but I thought of this off the top of my head.
It uses two files: one with link to the .pdf file, the other a counter.
1) 'download.php'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Download Script</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
// Change THIS to your file name!
$file = 'sample.pdf';
$phpcounter = 'counter.php';
$now = date('F dS, Y (g:i a)');
if(isset($_GET['action']) && $_GET['action'] == 'download'){
$handle = fopen($phpcounter, 'r');
$oldcontents = fread($handle, filesize($phpcounter));
fclose($handle);
$handle = fopen($phpcounter, 'w');
$newcontents = str_replace('?>', '', $oldcontents);
$newcontents .= '$downloads[] = \'' . $now . '\';' . "\n";
$newcontents .= '?>';
$results = fwrite($handle, $newcontents);
fclose($handle);
?>
<iframe name="box" src="<?php echo $file; ?>" height="0" width="0" frameborder="0"></iframe>
<p style="margin:150px;font-family:arial;text-align:center;font-size:12px;">
[<a href="javascript:;" onClick="self.close();return false">Close Window</a>]</p>
<?php
}
else {
include_once $phpcounter;
$count = (count($downloads) - 1);
?>
<p><a href="<?php echo $_SERVER['PHP_SELF'] . '?action=download'; ?>" target="box">Download file now!</a>
[<small>Downloaded <?php echo $count; ?></small> times]</p>
<?php } ?>
</body>
</html>
2) 'counter.php'
<?php
$downloads[] = 'NULL';
?>
The idea is that if the page is just visited, the counter ('counter.php') will be included and the contents read as an array. The array items will be counted and echoed onto the page.
However, if the link to download the file is clickec, a new window opens, the download begins in a hidden iframe, and the counter ('counter.php') will be updated with a timestamp, so you will know how may downloads AND when they were downloaded.
I hope this gives you a starting point.
Dear Rodney
thanks for your help, I'm lucky to have found a expert. Here my questions:
a) My page looks like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>page with download</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<a href="file_to_download.pdf">download file</a>
</body>
</html>
Dear Rodney
thanks for your help, I'm lucky to have found a expert. Here my questions:
a) My page looks like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>page with download</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<a href="file_to_download.pdf">download file</a>
</body>
</html>
Hello, Paldo:
All you have to do is:
1) Copy and paste the code I gave you in the first example into a new file, and save it with whatever name you like, for example: "download.php"
2) Then change the (twelfth) line of code in that file from this:
[INDENT]$file = 'sample.pdf'; [/INDENT]
To this:
[INDENT]$file = 'file_to_download.pdf'; [/INDENT]
3) Save the file.
4) Create a new file called "counter.php" and save it.
4) Add these lines of code (and nothing more) to it:
[INDENT]<?php
// track download times
$downloads[] = '';
?> [/INDENT]
That is it. Then give it a try. Does that answer your questions?
Dear Rodney
thanks for your help, I'm lucky to have found a expert. Here my questions:
a) My page looks like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>page with download</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<a href="file_to_download.pdf">download file</a>
</body>
</html>
Hello, Paldo:
All you have to do is:
1) Copy and paste the code I gave you in the first example into a new file, and save it with whatever name you like, for example: "download.php"
2) Then change the (twelfth) line of code in that file from this:
[INDENT]$file = 'sample.pdf'; [/INDENT]
To this:
[INDENT]$file = 'file_to_download.pdf'; [/INDENT]
3) Save the file.
4) Create a new file called "counter.php" and save it.
4) Add these lines of code (and nothing more) to it:
[INDENT]<?php
// track download times
$downloads[] = '';
?> [/INDENT]
That is it. Then give it a try. Does that answer your questions?
edit
I am sorry, Paldo, but I cannot write a script and help you install it into your page for you. If you want to learn, I will help you. But you must show some more initiative. I made some good suggestions to you. If you try to implement them, you will see how easy they are.
If not, just Google FREE counter and there are loads of pre-made counters out there for you. OK?
Thanks.
I understand your argument, you've helped me a lot already and more important, you opened my eyes and mind for php!! I will try to write a script myself. Thanks a lot.
paldo