PHP5 .tgz Download Counter
To add a download counter, you can use a RewriteRule to route requests through a script to deliver the file and update the counter. You may also be able to use a header(’location:file.tgz’) call as well.
.htaccess
RewriteEngine On
RewriteRule ^(.*\.tgz)$ download.php5?path=$1 [L]
download.php5
<?php
// downloading a file
$filename = $_GET['path'];
if (is_file($filename))
{
// fix for IE catching or PHP bug issue
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
// browser must download file from server instead of cache
// force download dialog
header("Content-Type: application/x-tgz");
// use the Content-Disposition header to supply a recommended filename and
// force the browser to display the save dialog.
header("Content-Disposition: attachment; filename=".basename($filename).";");
/*
The Content-transfer-encoding header should be binary, since the file will be read
directly from the disk and the raw bytes passed to the downloading computer.
The Content-length header is useful to set for downloads. The browser will be able to
show a progress meter as a file downloads. The content-lenght can be determines by
filesize function returns the size of a file.
*/
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
@readfile($filename);
$sCountFile=$filename.'.count.txt';
$iCount=file_get_contents($sCountFile);
file_put_contents($sCountFile,$iCount+1);
}
?>
Display Logic
<p><strong><?php include 'tri.tgz.count.txt'; ?></strong> downloads.</p>
This can be added to a site without changing the download URLs.
Many thanks to the PHP header link above, where the header code came from.
Print article | This entry was posted by elvis on 04/18/09 at 12:32:38 pm . Follow any responses to this post through RSS 2.0. |