Basically, I am planning to host photography e-books on my site and give them away for free.
I need a way to track these downloads and display the amount of downloads on my site, like one of those how many vistors widgets you see from time to time.
I will upload these to my server and display each e-book via a blog post site wide. So I would have about 100 blog posts in the end each with a e-book.
However, to encourage downloads I will also host ALL books in a zip file on one page. So if someone comes to my site they can just download all the files straight away.
Now, how would I track this? I will be adding files to the zip files constantly. I’m sure I could track the downloads for blog posts and display them quite easy but I was wondering if there was any software that could keep track of all blog post downloads and the zip files downloads and add them up and display them.
If someone where to download the zip file then that would be an extra (n) of books onto the total.
Anyway, i’m sure you know what I mean by now. Is there anything that can do this or do I need a custom php script or something?
If you just let the user to download the file directly, you can’t track the count of download. So, all the downloads happens through this program. Here now you have to ability increment the counter and store the update tracking count to db.
If you want to track the count for each file, just include the increment for each file by checking the file name in if conditions and increment the same in a table against the file name. Just read how to interact with database tables through php code.
Table structure can be:
id - int - primary key
filename - varchar
count - int
When you give link to a zip file, instead of mention it’s name, do like below:
<a href=‘downloadfile.php?filename=myfirstphotographybook.zip’>First Book.zip</a>
When user click on that link, downloadfile.php will be executed. In this php file, you have to write the code for incrementing the counter and give the requested file as download. Below is how you can achieve that:
<?php
//Content of downloadfile.php
$strRequestedFileName = $_REQUEST['filename'];
if (file_exists($strRequestedFileName)) {
//Increment the counter - You may choose to store this counter in a filed in db
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.basename($strRequestedFileName));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Content-Length: ' . filesize($strRequestedFileName));
readfile($file);
}
?>