Archive for the ‘PHP’ Category

PHP Random Image Script for Forum Signatures

September 28, 2004 in PHP | Comments (0)

I have a random image script that I use for a couple of forum signatures, and once in a while people ask me how I make that work. There are probably plenty of examples on the web, but this is the one that I cobbled together. Besides, I haven’t posted any PHP articles yet.

Just set the directory that you want to scan for images in the $dir variable, then save the file as sig.php or something.

In your signature settings, use a forum IMG tag, but for the url, put a reference to this script where you would ordinarily put the name of the image file. The script picks an image form the directory at random, then does a redirect to the file it chose.

I make a special images directory just to hold sigs, so you could have different sig directories for different forums if you want to, Just keep tossing images in the folder and they’ll show up in the rotation.

<?
  $dir = "/put/the/path/to/your/images/directory/here";
  $input = scandir("$dir");
  header ("Location: $dir/" . $input[array_rand($input)]);
  function scandir($dirstr)
  {
    $files = array();
    $fh = opendir($dirstr);
    while (false !== ($filename = readdir($fh)))
    {
      if ($filename != "." && $filename != "..")
      {
        array_push($files, $filename);
      }
    }
    closedir($fh);
    return $files;
  }
?>