8791 sujets

Développement web côté serveur, CMS

Modérateur
Bonjour,

Je souhaiterais remplacer le bout de code que je colle ci-dessous par le second, je voudrais me passer de la fonction glob() qui est désactivée sur les serveurs free.
Code à remplacer (les premières lignes) :

$folder_exists = false;
$folders = glob('plugins/avatarc/avatars/*');
foreach ($folders as $folder) {
        if (is_dir($folder)) {
                $folder_exists = true;
                $folder_name = str_replace('plugins/avatarc/avatars/', '', $folder);
                $avatars_list = '';
                //retrieving the avatars images
                $avatar_files = glob($folder.'/*');
                foreach ($avatar_files as $avatar_f) {
                         list($width, $height, $type) = @getimagesize($avatar_f);
                         // Test if the file is a valid image
                         if (@getimagesize($avatar_f) && $width <= $pun_config['o_avatars_width'] && $height <= $pun_config['o_avatars_height'] && $type < 4) { 
                                 $avatars_list .= '<img style="margin: 5px 0 5px 10px;" src="'.$avatar_f.'" width="'.$width.'" height="'.$height.'" alt="'.$lang_avatarc['Avatar'].'" title="'.$width.'x'.$height.' px" />';
                         }
                }
                echo '<form id="'.$folder_name.'" method="post" action="'.pun_htmlspecialchars($_SERVER['REQUEST_URI']).'#available_avatars">'."\n";
                echo '<fieldset style="border: #ddd solid 1px; background: #FFF; margin: 5px; padding: 5px;">'."\n";
                echo '<legend style="background: #ddd;margin: 2px 10px; padding: 2px 5px;">'.$folder_name.'</legend>'."\n";
                if ($avatars_list != '') {
                        echo '<p>'.$avatars_list.'</p>'."\n";
                        echo '<input type="hidden" name="folder" value="'.$folder.'" />'."\n";
                        echo '<p style="margin: 5px; padding: 5px; text-align: center;"><input type="submit" name="avatars_install" value="'.$lang_avatarc['Avatars install'].'" /> <input type="submit" name="avatars_remove" value="'.$lang_avatarc['Avatars remove'].'" />'."\n";
                } else {
                        echo '<p style="color: orange; font-weight: bold;">'.$lang_avatarc['No avatar'].'</p>'."\n";
                }
                echo '</fieldset>'."\n";
                echo '</form>'."\n";
                
        }
}
if (!$folder_exists) {
        echo '<p style="color: orange; font-weight: bold;">'.$lang_avatarc['No folder'].'</p>';
}		                

Code de remplacement :

$folder_exists = false;
$search_folder = opendir('plugins/avatarc/avatars');
while(false !== ($folder = readdir($search_folder))) {
if (is_dir($folder) && $folder != '.' && $folder != '..') {
                $folder_exists = true;
                $folder_name = str_replace('plugins/avatarc/avatars/', '', $folder);
                $avatars_list = '';
                //retrieving the avatars images
                $avatar_files = glob($folder.'/*');
                foreach ($avatar_files as $avatar_f) {
                         list($width, $height, $type) = @getimagesize($avatar_f);
                         $size = @filesize($avatar_f);
                         // Test if the file is a valid image
                         if (@getimagesize($avatar_f) && $width <= $pun_config['o_avatars_width'] && $height <= $pun_config['o_avatars_height'] && $type < 4 && $size <= $pun_config['o_avatars_size']) { 
                                 $avatars_list .= '<img style="margin: 5px 0 5px 10px;" src="'.$avatar_f.'" width="'.$width.'" height="'.$height.'" alt="'.$lang_avatarc['Avatar'].'" title="'.$width.'x'.$height.' px" />';
                         }
                }
                echo '<form id="'.$folder_name.'" method="post" action="'.pun_htmlspecialchars($_SERVER['REQUEST_URI']).'#available_avatars">'."\n";
                echo '<fieldset style="border: #ddd solid 1px; background: #FFF; margin: 5px; padding: 5px;">'."\n";
                echo '<legend style="background: #ddd;margin: 2px 10px; padding: 2px 5px;">'.$folder_name.'</legend>'."\n";
                if ($avatars_list != '') {
                        echo '<p>'.$avatars_list.'</p>'."\n";
                        echo '<input type="hidden" name="folder" value="'.$folder.'" />'."\n";
                        echo '<p style="margin: 5px; padding: 5px; text-align: center;"><input type="submit" name="avatars_install" value="'.$lang_avatarc['Avatars install'].'" /> <input type="submit" name="avatars_remove" value="'.$lang_avatarc['Avatars remove'].'" />'."\n";
                } else {
                        echo '<p style="color: orange; font-weight: bold;">'.$lang_avatarc['No avatar'].'</p>'."\n";
                }
                echo '</fieldset>'."\n";
                echo '</form>'."\n";
                
        }
}
closedir($search_folder);
if (!$folder_exists) {
        echo '<p style="color: orange; font-weight: bold;">'.$lang_avatarc['No folder'].'</p>';
}	

Le premier code liste bien les dossiers contenu dans le dossier plugins/avatarc/avatars/, en revanche, le 2e script ne renvoie pas de liste de dossiers (il faut savoir que dans le dossier de recherche, il n'y a que des dossiers). Est-ce qu'il n'est pas possible de lister des dossiers avec la fonction readdir() ?

Merci d'avance pour votre aide Smiley smile

PS : je sais qu'il y a encore un glob() à remplacer, mais je voulais d'abord réussir à remplacer le premier avant de faire la suite...
Modifié par jojaba (10 Jan 2011 - 21:33)
Modérateur
Bon ben, j'ai trouvé. En fait quand on récupère des dossiers avec glob() on obtient le chemin vers le dossier. Avec opendir() et readdir() ce n'est que le nom du dossier qui est retourné et non le chemin. Voici le code qui fonctionne :

$folder_exists = false;
						        $search_folder = opendir('plugins/avatarc/avatars');
						        while(false !== ($folder = readdir($search_folder))) {
						        if ($folder != '.' && $folder != '..' && is_dir('plugins/avatarc/avatars/'.$folder)) {
						                        $folder_exists = true;
						                        $avatars_list = '';
						                        //retrieving the avatars images
						                        $avatar_files = opendir('plugins/avatarc/avatars/'.$folder);
						                        while(false !== ($avatar = readdir($avatar_files))) {
						                                if ($avatar != '.' && $avatar != '..') {
						                                        $avatar_path = 'plugins/avatarc/avatars/'.$folder.'/'.$avatar;
						                                        list($width, $height, $type) = @getimagesize($avatar_path);
						                                        $size = @filesize($avatar_path);
						                                        // Test if the file is a valid image
						                                        if (@getimagesize($avatar_path) && $width <= $pun_config['o_avatars_width'] && $height <= $pun_config['o_avatars_height'] && $type < 4 && $size <= $pun_config['o_avatars_size']) { 
						                                                $avatars_list .= '<img style="margin: 5px 0 5px 10px;" src="'.$avatar_path.'" width="'.$width.'" height="'.$height.'" alt="'.$lang_avatarc['Avatar'].'" title="'.$width.'x'.$height.' px" />';
						                                        }
						                                }
						                        }
						                        closedir($avatar_files);
						                        echo '<form id="'.$folder.'" method="post" action="'.pun_htmlspecialchars($_SERVER['REQUEST_URI']).'#available_avatars">'."\n";
						                        echo '<fieldset style="border: #ddd solid 1px; background: #FFF; margin: 5px; padding: 5px;">'."\n";
						                        echo '<legend style="background: #ddd;margin: 2px 10px; padding: 2px 5px;">'.$folder.'</legend>'."\n";
						                        if ($avatars_list != '') {
						                                echo '<p>'.$avatars_list.'</p>'."\n";
						                                echo '<input type="hidden" name="folder" value="plugins/avatarc/avatars/'.$folder.'" />'."\n";
						                                echo '<p style="margin: 5px; padding: 5px; text-align: center;"><input type="submit" name="avatars_install" value="'.$lang_avatarc['Avatars install'].'" /> <input type="submit" name="avatars_remove" value="'.$lang_avatarc['Avatars remove'].'" />'."\n";
						                        } else {
						                                echo '<p style="color: orange; font-weight: bold;">'.$lang_avatarc['No avatar'].'</p>'."\n";
						                        }
						                        echo '</fieldset>'."\n";
						                        echo '</form>'."\n";
						                        
						                }
						        }
						        closedir($search_folder);
						        if (!$folder_exists) {
						                echo '<p style="color: orange; font-weight: bold;">'.$lang_avatarc['No folder'].'</p>';
						        }


Smiley cligne