Main Content

Script to present commonly used images - PHP

Archive - Originally posted on "The Horse's Mouth" - 2008-01-13 15:20:33 - Graham Ellis

"What are the most popular pages in your picture library, and where are they used from?" An interesting question and I could analyze raw web logs to find out the answers. However, most of our images are managed by a script that retains extra information such as an image title, and uses are logged ... so a simple PHP script will do the job admirably!

<?php
$most = fopen("../picfeed.txt","r");
 
while ($line = fgets($most,4096)) {
   $parts = explode(" ",$line);
   $counter[$parts[0]]++;
   }
 
arsort($counter);
$v = 0;
foreach (array_keys($counter) as $pname) {
   $html .= "$v: <img src=http://www.wellho.net/pix/$pname.jpg><br />";
   if (++$v > 10) break;
   }
?>
<html>
<head><title>Our most viewed pictures</title></head>
<body><h1>Most Viewed Pictures</h1>
Here are our most viewed pictures:<br>
<?= $html ?>
</body></html>


If you want to see our most popular images - dynamically - there's a link to that script running here. There's also an alternative version showing counts, tables of most popular referrers, and which allows you to page through the pictures page by page. You can run that alternative script here or here to show only images loaded by references from other sites and you can see the source code here. Passwords changed, of course.

Oh - <advert> I'll teach you how to do this on our PHP course! </advert>