Viewed   93 times

Please give me a solution for listing all the folders,subfolders,files in a directory using php. My folder structure is like this:

Main Dir
 Dir1
  SubDir1
   File1
   File2
  SubDir2
   File3
   File4
 Dir2
  SubDir3
   File5
   File6
  SubDir4
   File7
   File8

I want to get the list of all the files inside each folder.

Is there any shell script command in php?

 Answers

1
function listFolderFiles($dir){
    $ffs = scandir($dir);

    unset($ffs[array_search('.', $ffs, true)]);
    unset($ffs[array_search('..', $ffs, true)]);

    // prevent empty ordered elements
    if (count($ffs) < 1)
        return;

    echo '<ol>';
    foreach($ffs as $ff){
        echo '<li>'.$ff;
        if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
        echo '</li>';
    }
    echo '</ol>';
}

listFolderFiles('Main Dir');
Sunday, December 11, 2022
4

non-recrusive:

$dir = opendir('dir/');
$i = 0;
while (false !== ($file = readdir($dir))){
    if (!in_array($file, array('.', '..') and !is_dir($file)) $i++;
}

echo "There were $i files";

recrusive:

function crawl($dir){

    $dir = opendir($dir);
    $i = 0;
    while (false !== ($file = readdir($dir)){
            if (is_dir($file) and !in_array($file, array('.', '..'))){

            $i += crawl($file);
        }else{
            $i++;
        }

    }
    return $i;
}
$i = crawl('dir/');
echo "There were $i files";
Monday, August 15, 2022
 
aillyn
 
2

Try using absolute file paths if your PHP scripts can be invoked (or requested) from different directories. For example, the current directory of a PHP script executed under example.com/one.php will be different from example.com/folder/two.php.

Your change to

$config['etc'] = parse_ini_file('../config/config.txt');

is still relative, except this time it's just "one directory up from the current directory > config > config.txt", rather than before, where you were looking in the same current directory.

A generic fix would be to do the following:

$root = $_SERVER['DOCUMENT_ROOT'];
// ... code ...
$config['etc'] = parse_ini_file($root . '/config/config.txt');

There are possible issues with using the DOCUMENT_ROOT field, but it should be fine for the most part (such as DOCUMENT_ROOT not being defined under the CLI). Also, check if DOCUMENT_ROOT has a trailing slash, even though the standard is not to have it, some hosts may have the trailing slash included. If it does, remove your slash at the beginning of your file path.

Friday, September 23, 2022
 
3

Don't bother with open/readdir and use glob instead:

foreach(glob($log_directory.'/*.*') as $file) {
    ...
}
Saturday, October 29, 2022
 
mvark
 
5

Assuming you are trying to access the remote server using HTTP, you won't be able to do this directly. Since HTTP is really going access the web service of your remote server, it controls how the files are presented to you. In the case of many web servers, you can turn on "indexes" for folder contents. This causes the entries in the folder to be displayed in your browser as HTML. In order to traverse the directory structure, you would need to parse this HTML to find the path information.

If you are using FTP, you can pass the ftp://... URL to opendir() as of version 5.0. Note that this capability can be turned off by your server admin. If you cannot use this feature directly, see the FTP functions manual for PHP (including ftp_nlist() for listing files).

An example from the above references:

<?php

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// get contents of the current directory
$contents = ftp_nlist($conn_id, ".");

// output $contents
var_dump($contents);

?>
Saturday, September 3, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :