communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "file handling" - download "download counter"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

file handling - download counter was created/last modified on Thu 08 Jul 2004 9:39 AM

this snippet will count downloads of files and helps to cloak the real address of the file downloaded.

call like "thisscript.php?get=thefile_to_download"


<?php

$download_dir
= 'downloads'; // the folder where the files are stored ('.' if this script is in the same folder)
$counter_dir = 'counters'; // the folder where your counter files are stored

/* Save this script as download.php
// each file to download must have a .txt-file called like "filename.ext.txt" in the 'counters' folder -
// call the counter e.g. like this: <? include("counters/filename.pdf.txt"); ?>
// download the file [download.php?get=name_of_file]*/

$path = $download_dir.'/'.$HTTP_GET_VARS['get'];

if(
file_exists($path))
{
    
$file = fopen($counter_dir.'/'.$HTTP_GET_VARS['get'].'.txt','r+');
    
$count = fread($file,100);
    
fclose($file); // closes file
    
$count++;
    
$file = fopen($counter_dir.'/'.$HTTP_GET_VARS['get'].'.txt','w'); // opens file again with 'w'-parameter
    
fwrite($file, $count);
    
fclose($file);

    
$size = filesize($path);
    
    
header('Content-Type: application/octet-stream');
    
header('Content-Disposition: attachment; filename='.$HTTP_GET_VARS['get']);
    
header('Content-Length: '.$size);

    
readfile($path);  

}else{

   echo
"<font face=$textfont size=2>";
   echo
"<center><br><br>The file [<b>$get$extension</b>] is not available for download.<br>";
   echo
"Please contact the web administrator <a href='http://www.yoursite.com</a>here";
}


?>

learn more about some of the PHP functions used in this snippet: count, dir, each, echo, else, fclose, file, file_exists, filesize, fopen, for, fread, fwrite, header, include, min, readfile

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

go to PHParadise to get free PHP, Javascript and FlashMX code