Counting the download of jpegs

Does anyone know a simple way to count the number of times a particular jpeg is requested from the server?

My site uses flash 6, and the main .swf allows users to bring up jpegs dynamically. I just want to be able to see how often a .jpeg is called.

Dreamhost support told me to post this question here, because they seem not to know of a way to do it. This seems strange, as my previous site was structured identically to the current one (except that .swfs were called dynamically instead of jpegs), and the .swfs successfully showed up in my stats.


You’d probably have to write a script to look in the logs for your domain and save stats on this.

[quote]You’d probably have to write a script to look in the logs for
your domain and save stats on this.


It’s probably be even easier and perhaps a bit more reliable to write a script that you link to instead of the image itself which increments the count in a database or flat file and then outputs the image itself.

It would be called like this:

  • Jeff @ DreamHost
  • DH Discussion Forum Admin

Hey, if you write that sort of script (which outputs your image directly, setting Content-type correctly first of course!), you can even keep track of HTTP REFERRERs, and prevent nasty people from nastily stealing your bandwidth by directly linking to your images!

Just my $0.02. :slight_smile:

WebID: idorosen Donate.

Why hello, Ido, it’s me…you know, you!
(End of Schizophrenic Episode)

Use the below script as such:

Replacing imagename with the short name you gave the image in your MySQL table. :slight_smile:

Here’s a little bit of PHP for you to nibble on:

<?php // REAL QUICK SCRIPT TO SERVE IMAGES, LOGGING USAGE. // Copyright 2004 (c) Ido Rosen [licensing AT idorosen DOT com] /* License is hereby granted to all DreamHost customers for the use of this PHP script provided appropriate credits are made on the website this is used on, with a link to . Alternatively, license is hereby granted to all those who donate $10 or more to paypal AT idorosen DOT com via PayPal. In this case, your license ID is your transaction ID, and no credits are necessary on your website. :) Ido Rosen (licensing AT idorosen DOT com) */ /* In a MySQL database, create this table: CREATE TABLE images ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL UNIQUE, src VARCHAR(255) NOT NULL UNIQUE, downloads INT NOT NULL DEFAULT 0, INDEX(name), INDEX(src) ); -- Note: name is the short name for the image, -- id is a numeric id you can also use to refer to the image. -- src is the actual path to the image (which can be outside -- of your web directory, since only the php script will read it) -- Note2: If src is outside of your domain's web directory, -- then visitors can absolutely not access images without using -- this PHP script interface. -- name can have letters (a-z, A-Z), numbers (0-9), dash (-), -- underscore (_), or spaces. nothing else! -- You must then populate the table as follows: INSERT INTO images (name, src) VALUES ("me", "/home/myusername/images/itsme.jpg"); -- ... for each image you wish to serve. */ global $dbhost, $dbuser, $dbpass, $dbname; // Replace these values with the appropriate ones before using! $dbhost = ""; $dbuser = "yourmysqlusername"; $dbpass = "yourmysqlpassword"; $dbname = "yourdatabasename"; // look at the regex'es below -- they both check for id and name, // and ensure the security of your site files. if (preg_match('/^[0-9] $/',$_GET['id'])) { // use id to find image! show_image_by_id($_GET['id']); } elseif (preg_match('/^[a-zA-Z0-9\- _] $/',$_GET['name'])) { // use short name to find image! show_image_by_name($_GET['name']); } else { die("Invalid image id or name."); } // MySQL Database Connection to retrieve src location. function connect_to_db() { global $dblink; global $dbhost, $dbuser, $dbpass, $dbname; $dblink = @mysql_pconnect($dbhost, $dbuser, $dbpass); if (!@mysql_ping($dblink)) { die("Couldn't connect to database."); } if (!@mysql_select_db($dbname, $dblink)) { die("Couldn't USE database."); } return $dblink; } function show_image_by_id($id) { $db = connect_to_db(); $r = @mysql_query("SELECT src FROM images WHERE id=$id"); if (@mysql_num_rows($r) == 1) { $data = mysql_fetch_assoc($r); display_img($data['src']); } else { header("Status: 404 Not Found"); die("Image not found."); } } function show_image_by_name($name) { $db = connect_to_db(); $r = @mysql_query("SELECT id,src,downloads FROM images WHERE name='$name'"); if (@mysql_num_rows($r) == 1) { $data = mysql_fetch_assoc($r); display_img($data['src'], $data['id'], $data['downloads']); } else { header("Status: 404 Not Found"); die("Image not found."); } } function display_img($src, $id, $downloads) { // this function actually retrieves the image file and // spits it out, setting the Content-type appropriately... switch(strtolower(substr($src,-3))) { case 'jpg': header("Content-type: image/jpeg"); break; case 'gif': header("Content-type: image/gif"); break; case 'png': header("Content-type: image/png"); break; case 'tif': header("Content-type: image/tiff"); break; default: die("Unknown image type!"); // you can add MIME types to this list if you get this error. } // display the image to the user. print file_get_contents($src); // update the downloads count in the database; $downloads = $downloads 1; update_download_count($id,$downloads); // and we're done! } function update_download_count($id,$downloads) { global $dblink; @mysql_query("UPDATE images SET downloads = $downloads", $dblink); @mysql_close($dblink); } // Note: The above is pretty darn crude and educational, // but it works. // THE END. Brought to you by the letters "i", "d", and "o". :) WebID: idorosen [url=]Donate.[/url]