Hi Everyone,
I'm very new to PHP. I'm a designer looking to learn some basic skills.
Meanwhile, I've been searching for snippets of code.

I found this snippet to randomly rotate out header images. The problem is that I think it's old...the date on it is 2003, and it seems to get stuck on the same one too often. I know random can mean, it can pick the same one a few times in a row, but it seems to be happening too much. Any help would be appreciated.
Thanks

<?php
/*

*/// Make this the relative path to the images, like "../img" or "random/images/".
// If the images are in the same directory, leave it blank.
$folder = '';

// Space seperated list of extensions, you probably won't have to change this.
$exts = 'jpg jpeg png gif';

$files = array(); $i = -1; // Initialize some variables
if ('' == $folder) $folder = './';

$handle = opendir($folder);
$exts = explode(' ', $exts);
while (false !== ($file = readdir($handle))) {
foreach($exts as $ext) { // for each extension check the extension
if (preg_match('/\.'.$ext.'$/i', $file, $test)) { // faster than ereg, case insensitive
$files[] = $file; // it's good
++$i;
}
}
}
closedir($handle); // We're not using it anymore
mt_srand((double)microtime()*1000000); // seed for PHP < 4.2
$rand = mt_rand(0, $i); // $i was incremented as we went along

header('Location: '.$folder.$files[$rand]); // Voila!
?>

    I might do something like this:

    <?php
    // Relative or absolute path to image directory, no trailing slash
    $dir = "images";
    
    // array of allowed file name suffixes: may be case-sensitive on some OS's
    $extensions = array('jpg', 'jpeg', 'png', 'gif');
    
    $pattern = sprintf('%s/*.{%s}', $dir, implode(',', $extensions));
    $files = glob($pattern, GLOB_BRACE);
    if(empty($files))
    {
       notFound();
    }
    $file = $files[array_rand($files)];
    $info = getimagesize($file);
    if($info == false)
    {
       notFound();
    }
    header('Content-Type: ' . $info['mime']);
    header('Content-Length: ' . filesize($file));
    readfile($file);
    exit;
    
    /**
     * Output 404 header if problem:
     */
    function notFound()
    {
       header('HTTP/1.0 404 Not Found');
       exit;
    }
    

    By using readfile() instead of a location header(), this should eliminate one unneeded round-trip HTTP response/request.

      When I tried this the header area comes up blank.
      In learning more about PHP, I think the orginal code was written in PHP4 or earlier and needs to upgrades to PHP5.

      Thanks for your help though.

        What NogDog wrote above will work in both PHP 5 and PHP 4.3. One possible issue is that his code initialised $dir to "images" so that [man]glob[/man] looked in the images subdirectory instead of "." (the same directory as the script). The "." would be necessary, because a blank would cause glob to look in the server's root directory - and the images aren't likely to be there.

          Weedpacket;10951855 wrote:

          What NogDog wrote above will work in both PHP 5 and PHP 4.3. One possible issue is that his code initialised $dir to "images" so that [man]glob[/man] looked in the images subdirectory instead of "." (the same directory as the script). The "." would be necessary, because a blank would cause glob to look in the server's root directory - and the images aren't likely to be there.

          Good catch. I suppose it could be made a bit more robust with something like:

          if(empty($dir))
          {
             $dir = '.';
          }
          

          🙂

            Write a Reply...