title: How do I script a php file to display an image like <img src="/img.php?imageID=32" />?
body: <p>I'm storing banner ads in a mysql table or I should say just the file name e.g: 'imagename.jpg' or 'banner3.gif'. </p>
  
  <p>Obviously I am wanting to track when these banners are loaded so I'm thinking to create a php file called img.php and call it like so</p>
  
  <pre><code>&lt;img src="/img/img.php?imageID=3" /&gt;
  </code></pre>
  
  <p>And that would pull out the image with the id 3 along with updating my hits table etc.</p>
  
  <p>I know how to update my hits table... but what I'm trying to work out is how to code the img.php file so that it simply retrieves the file name and prints it to screen so it works like a regular image.</p>
  
  <p>So far I've got...</p>
  
  <pre><code>&lt;?php
  header("Content-Type: image/jpeg");
  // insert my db connect file
  // update the hits table etc
  
  $sql = 'SELECT * FROM ads WHERE id="' . $_GET['imageID'] .  '" ';
  $res = mysql_query($sql);
  
  $row = mysql_fetch_array($res);
  
  $image = '/img/' . $row['file'];
  
  imagejpeg($image);
  
  // and this is where I'm at...
  
  ?&gt;
  </code></pre>
  
  <p>I have some problems with the above code which i've extracted from various places to get here. The images could be png/gif/jpg while the header content-type seems to only have space for one type.</p>
  
  <p>Is there a way to have this one file work ok for multiple image types? I'm thinking I should query the table firstly, work out the file extension and then insert the header function based off that.</p>
  
  <p>But what do I actually do when I've got that right and I want the image to then just appear?</p>
  
  <p>thanks to your help here is the final working file</p>
  
  <pre><code>&lt;?php
  
  // make sure we're only getting a number for the query b4 doing stuff
  if(is_numeric($_GET['imid'])) {
  
  include('thedbfile.php');
  
  $types['jpg'] = 'image/jpeg';
  $types['png'] = 'image/png';
  $types['gif'] = 'image/gif';
  
  
  
  $sql = 'SELECT file FROM ads WHERE id="' . $_GET['imid'] . '" ';
  $res = mysql_query($sql);
  
  $row = mysql_fetch_array($res);
  
  $image = 'img/banners/' . $row['file'];
  
  $extension = end(explode('.', $row['file']));
  
  
  header('Content-Type: ' . $types[$extension]);
  
  echo file_get_contents($image);
  
  
  }
  
  ?&gt;
  </code></pre>