listing question

Discussion in 'PHP' started by izlik, Jul 25, 2009.

  1. #1
    Hello.

    I wonder how i can list the content of a folder on my server? let's say i have a folder called "images" on my server and i want to list all folder names inside it with a link. so if "art" and "wallpaper" is inside "images" i want it to be put out like this

    <a href="http://mysite.com/art">Art</a>
    <a href="http://mysite.com/wallpaper">Wallpaper</a>

    is this possible to do ? i hope someone can help out.
     
    izlik, Jul 25, 2009 IP
  2. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    yes possible :)

    place this code in a php file where your images folder resides and see if this is what you are looking for.

    <?php
    function get_folders_list($path_to_folder_of_folders){
    	$dir_handle = @opendir($path_to_folder_of_folders) or die("Unable to open $path_to_folder_of_folders");
    	$folders = "";
    	while ($directory = readdir($dir_handle))
    	{
    		if($directory != "." && $directory != "..") 	
    			if (is_dir($path_to_folder_of_folders.$directory)){
    				$folders .= '<a href="http://mysite.com/'.$directory.'">'.$directory."</a><br />";
    			}	
    	}
    	closedir($dir_handle);
    	return $folders;
    }
    
    echo get_folders_list("images/");
    
    ?>
    PHP:
    best luck :cool:
     
    zeronese, Jul 25, 2009 IP
  3. izlik

    izlik Well-Known Member

    Messages:
    2,399
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    185
    #3
    that's indeed what i want! thank you very very much. I have one addition though if you think you can help me out here. inside every folder that's displayed by the code bellow there is even more folders :p now im wondering if it's possible to count them and display the resault? the example then being

    <a href="http://mysite.com/art">Art</a>: 59 subfolders
    <a href="http://mysite.com/wallpaper">Wallpaper</a> 5 subfolders

     
    izlik, Jul 25, 2009 IP
  4. zeronese

    zeronese Peon

    Messages:
    83
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    glad it worked for you.
    it is easy to make links for subdirectories also by making a recursive call inside the function.
    so your function would like this:

    <?php
    function count_sub_folders($path_to_folder){
    	$dir_handle = @opendir($path_to_folder) or die("Unable to open $path_to_folder_of_folders");
    	$folders = 0;
    	while ($directory = readdir($dir_handle))
    	{
    		if($directory != "." && $directory != "..") 	
    			if (is_dir($path_to_folder.$directory)){
    				$folders++;
    			}	
    	}
    	closedir($dir_handle);
    	return $folders;
    }
    
    function get_folders_list($path_to_folder_of_folders){
    	$dir_handle = @opendir($path_to_folder_of_folders) or die("Unable to open $path_to_folder_of_folders");
    	$folders = "";
    	while ($directory = readdir($dir_handle))
    	{
    		if($directory != "." && $directory != "..") 	
    			if (is_dir($path_to_folder_of_folders.$directory)){
    				$folders .= '<a href="http://mysite.com/'.$directory.'">'.$directory." (".count_sub_folders($path_to_folder_of_folders."/".$directory."/")." Subfolders)</a>"."<br />";
    				$folders .=  get_folders_list($path_to_folder_of_folders."/".$directory."/");
    			}	
    	}
    	closedir($dir_handle);
    	return $folders;
    }
    
    echo get_folders_list("images/");
    ?>
    PHP:
    now this will list directories, sub-directories and even sub-sub directories all with counts....:cool:
     
    Last edited: Jul 25, 2009
    zeronese, Jul 25, 2009 IP