#!/usr/bin/perl # #Count_downloads.cgi # #Dave Vogel - dave@vogelnet.com - 4/5/2001 # # Simple PERL script to keep track of the number of requests for a download file. # Call this script with the argument of the path to the file. The script # looks for the count file in the directory with the file, if not there it creates # one with the same file name as the download file but with a .txt extention. Warning: # the path to the D/L file is the web server path (i.e. the path you would type into # your browser. The script below has to know the REAL path to that directory so that # the text count file can be written there. Fill in the REAL path to the root # directory of your web server in the variable below $pathToWebRoot. # #Example call: # in your HTML code where you ordinarily would have the image tag: # # # # Put the following in place of the href="myDownloadFile.ext" above: # # href="/cgi-bin/count_downloads.cgi?/home/directoryName/somePath/myDownloadFile.ext" ########User modified variables - must be editted before using script $pathToWebRoot = "/00vogelnet/public_html"; #REAL path to the root directory of your server #pathToWebRoot = "/home/vogelnet/public_html"; ########End of user modified variables $downloadFileWithPath = $ARGV[0]; #Path to the image file from calling doc #Separate the filenames from extensions and paths ($downloadFilename, $downloadFileExt, $downloadFilePath) = findPath($downloadFileWithPath); #Make the count filename same but with .txt extention $countFile = $pathToWebRoot.$downloadFilePath.$downloadFilename.".txt"; #Increment the count if (!open CNT, $countFile) { #Check to see if the count file exists close CNT; #Necessary? open CNT, ">".$countFile; #If not, create it (open for output) $mode=0666; chmod $mode, $countFile; # make it writeable print CNT "0"; # and start the count at zero close CNT; } open CNT, "+<".$countFile; #Now this should exist, so open for update flock CNT, LOCK_EX; #Prevent another invocation of this script from # changing things (lock count file) read CNT, $count, 10; #Get the old count value $count++; # and increment it seek CNT, 0, 0; #Go back to the beginning of the file print CNT $count; # and write back the new value flock CNT, LOCK_UN; #Allow updates now (unlock count file) close CNT; #Now print the page to redirect to the download file print <Download $downloadFilenameDotExt
If the download does not start in a few seconds
click
here.
endDownloadPage sub findPath { my ($fileWithPath) = @_; $fileWithPath =~ s![^\w\s\./_-]!!g; #Only allow alphanumerics, spaces, periods # and slash, underscore, colon and hyphen $fileWithPath =~ m!([^/]+)$!; #Match what is after the last slash $filenameDotExt = $1; # filename.ext $filePath = $`; #Everything before the file name (ie. the path) $filenameDotExt =~ /(.+)\.([^\.]+)$/; #Separate the file name from the extention $filename = $1; # just the name (what is before the last dot) $fileExt = $2; # just the extention (what is after the last dot) return ($filename, $fileExt, $filePath) ; }