hi..please help me to have code that will display list of folders
from a given directory in php or javascript.

for example i want to display in a page the list of folders
in my c:\Program Files.

Thanks in advance.

Recommended Answers

This would be a good place to start.

http://php.net/manual/en/function.scandir.php

This function will let you scan a directory, but you have to create your own methods for determining the difference between files and folders. …

Jump to Post

Here is some sample code with explanations. Make sure to run and check the output yourself. This is most definitely not the best or most perfect, but it's a good start.

$path = "/path/"; //<-the path that you want
$folderList = array(); //holder for folders
$directories = scandir( realpath($path) ); …
Jump to Post

That link has all the sample codes you need. Copying an dpasting them here won't make much difference. Try one of them. See what happens.

Jump to Post

All 10 Replies

This would be a good place to start.

http://php.net/manual/en/function.scandir.php

This function will let you scan a directory, but you have to create your own methods for determining the difference between files and folders. Those are also PHP standard functions, so please look through the documentation.

hi nexocentric.
i looked over the site you gave me.but i saw it complicated.
sorry.im kinda new to this.can you please give me sample code?

hope you can help me. thanks again.. :-(

Here is some sample code with explanations. Make sure to run and check the output yourself. This is most definitely not the best or most perfect, but it's a good start.

$path = "/path/"; //<-the path that you want
$folderList = array(); //holder for folders
$directories = scandir( realpath($path) ); //adjusts your path for UNIX or Windows and scans a directory
//go through the list and check if it's a directory
foreach( $directories as $folder ) {
    if ( is_dir($path.$folder) ){
        //add if directory
        $folderList[$folder] = $folder; //after this is done folderList for various things
    }
}
Member Avatar

That link has all the sample codes you need. Copying an dpasting them here won't make much difference. Try one of them. See what happens.

hi nexocentric..
i tried the code.. then i put $path="C:/Program Files/"
nothing happens..
sorry..what can you suggest??

thanks :-(

Is your computer you are running this code from a windows system? localhost?
Or have you uploaded it to a server, and want to read the dir of the computer that runs your browser?

$files = array(  ); // or a class object
$dir = opendir( $name );
while( false !== ($file = readdir($dir)) )
{
  if( $file != '.' && $file != '..' )
  {
    $pop = explode('.',$file);
    $ext = "";
    if(count($pop)>1) $ext = array_pop($pop);
    $files[implode('.',$pop)][] = $ext; // You may want to save other attributes as well, shrug
  }
}
closedir($dir);

hi.. its not working.
i already tried this one but it always reads the web server not the other remote server:

<?php
    $ip = 'ip.add.re.ss'; //this is the server i want to connect
    $host = gethostbyaddr( $ip );
    if ( $ip == $host )
        die( 'Unable to resolve hostname from ip '.$ip );

    $path = '//'.$host.'/sharename';


    if ( !is_dir($path) )
        die( $path. ' is not a directory' );

    $dir = opendir($path);
    if ( $dir == FALSE )
        die( 'Cannot read '.$path );

    while (($file = readdir($dir)) !== FALSE)
        echo "filename: $file : filetype: ".filetype( $path.$file)."\n";

    closedir( $dir );


?>

please help me..thanks..

What do you mean by "other remote server". You cannot use opendir and readdir across servers (unless you use ftp).

other server from network... :-)

i also tried:

<?php
    $ftp_server = "//ip.ad.dre.ss";
    $conn_id = ftp_connect($ftp_server);
    $ftp_user_name = "root";
    $ftp_user_pass = "password";
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
    $contents = ftp_rawlist($conn_id, '/');
    for ($i = 0 ; $i < count($contents) ; $i++)
     {
        echo "<li>" . substr($contents[$i],1) . "</li>";
        }
    ftp_close($conn_id);
?>

it still didnt work for me. :-(
i even tried using the hostname in $ftp_server.
also tried with "/" at the end of the $ftp_server.
still cannot connect.

whats wrong?hope you can help me..thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.