Script Help Please

Discussion in 'Programming' started by geekazoid, Jun 29, 2006.

  1. #1
    On a differant post here i was given this code to make a random image desplayed on my page each day, not each page load. The problem is i've tried using it and i just cant seem to get it to work, I dont know if the script is dodgey or im justing puting it in the wrong place. Heres the script

    <?phpfunction getPotD($dir) {    $array = split(' ', shell_exec('ls -A '.$dir));    return $array[(date('dd')+0) % count($array)];}?>
    PHP:
    and also

    <?php print '<img src="'.getPotD('certaindir/').'" alt="Picture of the Day" />'; ?>
    PHP:
    Could someone tell me please where in my page i place this script and which bits i need to edit, I believe its just the certaindir bit that i edit is this correct ?
     
    geekazoid, Jun 29, 2006 IP
  2. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I would approach it a little differently. You should not be using a shell_exec to do this.

    
    Include this is the file the user is fetching:
    
    <?php
    include_once( '/path/to/my_functions.php' );
    
    print '<img src="'.getTodaysPicture('/path/to/pictures/').
       '" alt="Picture of the Day" />';
    ?>
    
    Save the following into a php function file such as:
    /home/MY_HOME/php_functions/my_functions.php
    
    <?php
    function getTodaysPicture($dir)
    {
    $dh  = opendir($dir);
    while (false !== ($filename = readdir($dh)))
       {
       if( !is_dir($dir . $filename) &&
           preg_match( "/\.(gif|png|jpeg)/", $filename) )
    	{    $files[] = $filename; }
       }
    
    return $files[(date('dd')+2) % count($files)];
    }
    ?>
    
    Code (markup):
     
    clancey, Jun 30, 2006 IP