Does anyone know how I can count how many times a pdf has been downloaded from my site? Maybe someone has a php script or something that does this?

Thanks!

Dave

If you running PHP enabled server you can use Webalizer tool. Correctly configured it will provide in deep view/report on your website activity

I think it would be better to move this thread to PHP Forum, in order to get more response from others.

peter_budo: that looks cool, but seems like WAY overkill for what i'm trying to do

if this can indeed be done with a simple php script, can one of the admins please move this to php as mexabet suggested?

Thanks,

Dave

I have 0 php experience but this doesn't seem like it would be that hard to do. Just have it read the last 3 characters of the file that was downloaded and if all the characters are pdf then you accumulate some variable that keeps track of number of downloads.

That is not over kill, it standard tool use for monitoring website activity and utilizing variety of data in nice neat tables and graphs

Anyway post been move to PHP

I use this script for downloading pdfs from an ftp site because they are secure files, you can just delete this part(ftp://username:password@servername) out and just use $docname to pull a file from the file system. Then all you need is some type of database connection which manages your statistics and you're all set:

<?
$docname = "/path/to/file.pdf";
$docsize=@filesize("ftp://username:password@servername$docname");

if($docsize == 0 || $docsize == -1)
{
	header("location: http://" . $_SERVER['SERVER_NAME'] . "/index");
	exit();
}
else
{
	$fp=fopen("ftp://username:password@servername$docname", "rb");
	header("Pragma: ");
	header("Cache-Control: ");
	header("Content-type: application/pdf");
	header("Content-Disposition: attachment; filename=\"" . basename($docname) . "\"");
	header("Content-Transfer-Encoding: binary");
	header("Content-length: $docsize");
	fpassthru($fp);
	//header("Connection: close");
}
?>

One small adjustment: the return is not correct if the file is not found. adjusted the if statement.

<?
$docname = "/path/to/file.pdf";
if(!$docsize=@filesize("ftp://username:password@servername$docname"))
{
	header("location: http://" . $_SERVER['SERVER_NAME'] . "/index");
	exit();
}
	
$fp=fopen("ftp://username:password@servername$docname", "rb");
header("Pragma: ");
header("Cache-Control: ");
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"" . basename($docname) . "\"");
header("Content-Transfer-Encoding: binary");
header("Content-length: $docsize");
fpassthru($fp);
//header("Connection: close");
?>

Sorry about that, as you can imagine I had to simplify the script a bit before posting. It works fine now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.