corz.org uses cookies to remember that you've seen this notice explaining that corz.org uses cookies, okay!
<?php
$version = '0.3';
/*
corz download counter script
adapted from the corzoogle download page, I wanted to keep track of the
downloads. for more functionality, multiple counters, etc, see "corz
distro machine". you may prefer this approach for one-page-one-download
scenarios, essentially adding a single php function to your page, to
deliver and count the downloads.
to the downloader it works just the same as getting a file the regular
way, except now we know how many times a file has been downloaded. you
could potentially insert any routine into the process. like the counter
is. you may want to display total downloads to the user, for instance.
inside a php echo, just do just do..
<a href="',$_SERVER['SCRIPT_NAME'],'?download=somefile.zip,'">
or inside plain html do..
<a href="<?php echo '$_SERVER['SCRIPT_NAME'],'?download=somefile.zip';?*">
(replacing "*' with ">" - I can't close the php, I'm IN it!)
:!: noobs:!:
this page sends a GET request to itself with the name of the file *in*
the request. when a download request is received, it does the
go_download function, increases the counter by one, and then dies
immediately.
:!: installation :!:
insert the three <?php parts into your page, top, link, and function.
:!: counter :!:
for the counters to work, you'll need to have a file called .ht_downloads
in the downloads folder. this file will need to be world- writeable.
(chmod 777 or use your FTP client to set the permissions to ALL)
© corz.org 2004
this goes at THE VERY TOP of your page. yes, above the header. */ ?>
<?php if (isset($_GET['download'])) {
ini_set('zlib.output_compression','off');
$file = str_replace('../','', @$_GET['download']);
go_download($file); die(); }?>
<?php /* note:
if you have zlib.output_compression set on your site (generally a smart idea)
it's best to disable it for this folder (in the relevant .htaccess file)
makes downloads go smoother, though they do work with it on, mostly. */ ?>
<?php // in your page, use a link something like this.. ?>
<?php echo '
<a href="',$_SERVER['SCRIPT_NAME'],'?download=somefile.zip" title="download this file">somefile</a><br />';
?>
<?php /* if short tags are enabled on your server <?='<a href="'... is fine, too */ ?>
<? /* somefile.zip lives in the same folder as your page, though you could, of
course, put it anywhere you like, perhaps in a protected subfolder.
there's a wee note about this inside the function (below). */ ?>
<?php /* this function goes at the very bottom of your page.. ;o) */ ?>
<?php
/*
function go_download() */
function go_download($download) {
// specify a folder for the file './' = current folder
$download_path = './';
/* you MUST specify a path to a download, or else folks will send you page requests
like /page.php?download=/config.php - you don't want that. */
$file= $download_path.$download;
// send the requested file to the user..
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=".$download);
header("Content-Length: ".filesize($file)."\n");
$get_it = fopen($file, 'rb');
fpassthru($get_it);
// now the counter..
$count_file = '.ht_downloads';
if (file_exists($count_file)) {
// read in the old score..
$count = implode('', file($count_file));
$count++;
// the writing..
$file_pointer = fopen($count_file, 'w+');
fwrite($file_pointer, $count);
fclose($file_pointer);
}
}/*
end function go_download()
*/
/*
version history
0.3
improved security
0.2
this has been more popular than I imagined, so I've imported some things
I learned coding corz distro machine. importantly, we now DO send file
sizes, and we DO disable zlib output compression, though you may still
want to do that in some local .htaccess file (if it's enabled in the
first place, that is)
a few other minor tweaks, improvements, etc.
0.1
ripped the function out of the corzoogle download page, put it out on its
own
*/
?>