Page 1 of 1

displaying directories problem

Posted: Sat Dec 02, 2006 11:17 am
by tymlls05
I have three folders in my "www" directory I cannot get to show from my php file.
I am trying to display them from a file located in "C:/wamp/www/wamp5/index.php".
If I don't ignore "." and ".." then "." and ".." are displayed as that.
If I do ignore . and .. then it echos this:

No project yet.
To create a new one, just create a directory in 'www'.


Which is called from a variable on another part of the page.

Code: Select all

<?php
                        $list_ignore = array ('.','..','test_perl','exemples','phpmyadmin','sqlitemanager');
                        $dir = "C:/wamp/www/";
                        $handle=opendir($dir);

                        $msg = $langues[$langue]['txt_no_projet'];
                        while ($file = readdir($handle)) {
                            if (is_dir($file) && !in_array($file,$list_ignore)) {    
                                $msg = '';
                                echo '<a class="ditem" href="'.$file.'"><img src="dossier.gif" alt="image dossier" /> '.$file.'</a>';
                            }
                        }
                        closedir($handle);
                        echo $msg;
                    ?>

Any help would be great.

Edit: if I place this file in the actual www folder (where I dont need it) and remove "$dir = "C:/wamp/www/";" and change $handle=opendir(.); it displays the folders.

Posted: Sat Dec 02, 2006 2:14 pm
by feyd
readdir() returns the file/directory name only. It must often be recombined with the original path you supplied to opendir() to get a path you can then use to check with is_file() and is_dir().

You may be interested in glob().

Posted: Sun Dec 03, 2006 12:27 pm
by tymlls05

Code: Select all

<?php
                        // Affichage de la liste des dossiers non énumérés plus haut ( = projets ).
                        $list_ignore = array ('db','test_perl','exemples','phpmyadmin','sqlitemanager');
                        $dir = "C:/wamp/www/";
                        $handle=opendir($dir);

                        $msg = $langues[$langue]['txt_no_projet'];
                        while ($file = readdir($handle)) {
                            if (is_dir($file) && !in_array($file,$list_ignore)) {    
                                $msg = '';
                                foreach (glob("../*.*") as $filename) {
                                echo '<a class="ditem" href="'.$filename.'"><img src="dossier.gif" alt="image dossier" /> '.$filename.'</a>'; 
}
                            }
                        }
                        closedir($handle);
                        echo $msg;
                    ?>
I'm guessing the way I have altered this from the orignal version I have half this script doing absolutely nothing to the echo, but I researched glob and it is echoing the right folder, but it is only echoing the files in the folder rather than the folders. I googled glob and I havent been able to find how to make it read directories instead of files. And it is repeating each file twice.

Code: Select all

../Thumbs.db
../index.php
../Thumbs.db
../index.php
I need it to display the directories in my specified folder anyways. Thanks.[/syntax]

Posted: Sun Dec 03, 2006 12:45 pm
by feyd
If you're simply needing a listing, you may want to look into a full tree example I've posted in Code Snippets:

viewtopic.php?t=47288

Posted: Sun Dec 03, 2006 12:50 pm
by tymlls05
i just discorvered in my original script that my ignore list was making it ignore all the files for some reason.

I couldn't figure out how to ignore regular files without ignoring everything all together, for some reason. so here is the script i'm going to have to live with. it is still displaying thumb.db and unneccessary files like that.

Code: Select all

<?php

                     		//$list_ignore = array ('.','..','test_perl','exemples','phpmyadmin','sqlitemanager');

			//define the path as relative
			$path = "C:/wamp/www";

			//using the opendir function
			$dir_handle = @opendir($path) or die("Unable to open $path");

			echo "Directory Listing of $path<br/>";

			//running the while loop
			while ($file = readdir($dir_handle))
			 {    
			
   				echo "<a class=ditem href='$file'/><img src=dossier.gif /> $file</a><br/>";
			}

//closing the directory
closedir($dir_handle);

                    ?>
it's not a big deal to me anymore since the directories are displaying now, and this can be a done topic. but if someone happens to get a spare second sometime i'd like an idea on how to ignore certain files with this script. thanks.