Title: Random logo script not working ?
Last modified: August 19, 2016

---

# Random logo script not working ?

 *  Resolved [JohnWinch](https://wordpress.org/support/users/johnwinch/)
 * (@johnwinch)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/)
 * Hello,
 * I would like to display a random logo from a specific folder. I’ve found the 
   following script, it’s perfectly working on a test.php located in my folder theme
   but nothing is displaying when I put the code in header.
 * Here’s the code :
 *     ```
       <?php
       // Ouvre un dossier bien connu, et liste tous les fichiers
       $directory = 'images/logo';
       // Définition d'$image comme tableau
       $image = array();
        //on vérifie s’il s’agit bien d’un répertoire
        if (is_dir($directory))
        {
        //on ouvre le repertoire
        if ($dh = opendir($directory))
        {
        //Lit une entrée du dossier et readdir retourne le nom du fichier
        while (($file = readdir($dh)) !== false)
        {
        // Vérifie de ne pas prendre en compte les dossier ...
        if ($file != '...' && $file != '..' && $file != '.')
        {
        // On ajoute le nom du fichier dans le tableau
        $image[] = $file;
        }
        }
        //On ferme le repertoire
        closedir($dh);
        // On récupère le nombre d'image total
        $total = count($image)-1;
        // On prend une valeur au hasard entre 1 et le nombre total d'images
        $aleatoire = rand(0, $total);
        // On récupère le nom de l'image avec le chiffre hasard
        $image_afficher = "$image[$aleatoire]";
        // Affiche l'image du hasard :p
        print "<img src='$directory/$image_afficher' border='0'>";
        }
        }
       ?>
       ```
   
 * Is there a part of this code incompatible with WordPress ?
 * Thanks you for your help.
 * Best regards,

Viewing 13 replies - 1 through 13 (of 13 total)

 *  [Kapil Chugh](https://wordpress.org/support/users/kapilchugh/)
 * (@kapilchugh)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/#post-1658020)
 * I think you need to change directory path to
    $directory = TEMPLATEPATH . ‘/images/
   logo’;
 *  Thread Starter [JohnWinch](https://wordpress.org/support/users/johnwinch/)
 * (@johnwinch)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/#post-1658064)
 * Thanks you for your answer but it didn’t work.
    Is there an another way to display
   randomly a logo from a folder ?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/#post-1658067)
 *     ```
       $directory = TEMPLATEPATH . '/images/logo';
       $directory_uri = get_stylesheet_directory_uri() . '/images/logo/';
       ```
   
 * […]
    `print '<img src="' . $directory_uri . $image_afficher . ' border="0" />';`
 *  Thread Starter [JohnWinch](https://wordpress.org/support/users/johnwinch/)
 * (@johnwinch)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/#post-1658074)
 * Thanks, I’ve tried it but it didn’t work either.
    Here’s my code, i don’t think
   I’ve made any mistakes.
 *     ```
       <?php
       // Ouvre un dossier bien connu, et liste tous les fichiers
       $directory = TEMPLATEPATH . '/images/logo';
       $directory_uri = get_stylesheet_directory_uri() . '/images/logo/';
       // Définition d'$image comme tableau
       $image = array();
        //on vérifie s’il s’agit bien d’un répertoire
        if (is_dir($directory))
        {
        //on ouvre le repertoire
        if ($dh = opendir($directory))
        {
        //Lit une entrée du dossier et readdir retourne le nom du fichier
        while (($file = readdir($dh)) !== false)
        {
        // Vérifie de ne pas prendre en compte les dossier ...
        if ($file != '...' && $file != '..' && $file != '.')
        {
        // On ajoute le nom du fichier dans le tableau
        $image[] = $file;
        }
        }
        //On ferme le repertoire
        closedir($dh);
        // On récupère le nombre d'image total
        $total = count($image)-1;
        // On prend une valeur au hasard entre 1 et le nombre total d'images
        $aleatoire = rand(0, $total);
        // On récupère le nom de l'image avec le chiffre hasard
        $image_afficher = "$image[$aleatoire]";
        // Affiche l'image du hasard :p
       print '<img src="' . $directory_uri . $image_afficher . ' border="0" />';
        }
        }
       ?>
       ```
   
 * Here’s the path to my theme file :
    [http://127.0.0.1/blog/wp-content/themes/fashionpro/](http://127.0.0.1/blog/wp-content/themes/fashionpro/)
 * My logo’s folder :
    [http://127.0.0.1/blog/wp-content/themes/fashionpro/images/logo/](http://127.0.0.1/blog/wp-content/themes/fashionpro/images/logo/)
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/#post-1658078)
 * Try this:
 *     ```
       <?php
       $directory = TEMPLATEPATH . '/images/logo';
       $directory_uri = get_stylesheet_directory_uri() . '/images/logo/';
       $image = array();
       if ( is_dir($directory) && $dh = opendir($directory) ) {
       	while (($file = readdir($dh)) !== false) {
       		if ($file != '...' && $file != '..' && $file != '.') {
       			$image[] = $file;
       		}
       	}
       	closedir($dh);
       	$total = count($image)-1;
       	$aleatoire = rand(0, $total);
       	$image_afficher = $image[$aleatoire];
       	print '<img src="' . $directory_uri . $image_afficher . ' border="0" />';
       }
       ?>
       ```
   
 *  Thread Starter [JohnWinch](https://wordpress.org/support/users/johnwinch/)
 * (@johnwinch)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/#post-1658082)
 * It didn’t work, but when I look at the source code from Firefox, here’s how the
   code is displayed :
 * <img src=”[http://127.0.0.1/blog/wp-content/themes/fashionpro/images/logo/l.jpg%20border=&#8221](http://127.0.0.1/blog/wp-content/themes/fashionpro/images/logo/l.jpg%20border=&#8221);
   0=””>
 * This must have something to do with the %20 after the .jpg
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/#post-1658084)
 * Check what’s in the $image array.
 *  Thread Starter [JohnWinch](https://wordpress.org/support/users/johnwinch/)
 * (@johnwinch)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/#post-1658095)
 * What should I be looking for ? I’m new to this but if you explain to me it might
   help me understand.
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/#post-1658099)
 *     ```
       <?php
       $directory = TEMPLATEPATH . '/images/logo';
       $directory_uri = get_stylesheet_directory_uri() . '/images/logo/';
       $image = array();
       if ( is_dir($directory) && $dh = opendir($directory) ) {
       	while (($file = readdir($dh)) !== false) {
       		if ($file != '...' && $file != '..' && $file != '.') {
       			$image[] = $file;
       		}
       	}
       	closedir($dh);
               echo '<pre>';
               print_r($image);
               echo '</pre>';
       	$total = count($image)-1;
       	$aleatoire = rand(0, $total);
       	$image_afficher = $image[$aleatoire];
       	print '<img src="' . $directory_uri . $image_afficher . ' border="0" />';
       }
       ?>
       ```
   
 *  Thread Starter [JohnWinch](https://wordpress.org/support/users/johnwinch/)
 * (@johnwinch)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/#post-1658105)
 * It’s not working but when I put my mouse over the place where the logo should
   be displayed I’ve got the following :
 * Array
    ( [0] => l.jpg [1] => logo1.jpg [2] => logo2.jpg [3] => logo3.jpg [4] 
   => zeatrt.jpg [5] => zergter.jpg [6] => zerzt.jpg )
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/#post-1658109)
 * Remove:
 *     ```
       echo '<pre>';
       print_r($image);
       echo '</pre>';
       ```
   
 * and change:
 * `print '<img src="' . $directory_uri . $image_afficher . ' border="0" />';`
 * to:
 * `echo '<img src="' . $directory_uri . $image_afficher . '" />';`
 *  Thread Starter [JohnWinch](https://wordpress.org/support/users/johnwinch/)
 * (@johnwinch)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/#post-1658115)
 * Thanks you ! It’s working !
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/#post-1658117)
 * Excellent! 🙂

Viewing 13 replies - 1 through 13 (of 13 total)

The topic ‘Random logo script not working ?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 13 replies
 * 3 participants
 * Last reply from: [esmi](https://wordpress.org/support/users/esmi/)
 * Last activity: [15 years, 8 months ago](https://wordpress.org/support/topic/random-logo-script-not-working/#post-1658117)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
