如何使用PHP回显特定文件夹中的所有文件夹名称?

时间:2022-09-23 21:15:44

I want to print the folder names in a particular folder. For example Folder 1,2,3 are in a folder named 'New' I want to print the folder names 1 2 3 using php. Is it possible ? is there any function in php to do that ? any kind of solutions or ideas are very much appreciated.

我想在特定文件夹中打印文件夹名称。例如,文件夹1,2,3位于名为“新建”的文件夹中。我想使用php打印文件夹名称1 2 3。可能吗 ?在PHP中有任何功能吗?非常感谢任何类型的解决方案或想法。

4 个解决方案

#1


1  

<?php
$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
        }
        closedir($dh);
    }
}
?>

credit to php.net

信用到php.net

Extending this logic,

扩展这个逻辑,

<?php
$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if(filetype($dir . $file) == 'dir') {
                echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
            }
        }
        closedir($dh);
    }
}
?>

should echo all directories

应该回显所有目录

The output on my local server is:

我本地服务器上的输出是:

filename: . : filetype: dir
filename: .. : filetype: dir
filename: apache : filetype: dir
filename: etc : filetype: dir
filename: pranav : filetype: dir

#2


1  

Here is some example code from php.net on the readdir() function:

以下是来自php.net的readdir()函数的一些示例代码:

<?php
if ($handle = opendir('.')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            echo "$entry\n";
        }
    }
    closedir($handle);
}
?>

If you modify this code to check $entry with the is_dir() function, you can easily find your directories.

如果修改此代码以使用is_dir()函数检查$ entry,则可以轻松找到目录。

#3


1  

<?php       

    // declare the folder    
    $ourDir = "/home/public_html/folderName";

    // prepare to read directory contents    
    $ourDirList = @opendir($ourDir);

    // loop through the items    
    while ($ourItem = readdir($ourDirList))    
    {        
       // check if it is a directory    
       if (is_dir($ourItem))    
       {    
          echo "directory: $ourItem <br />";    
       }

       // check to see if it is a file    
       if (is_file($ourItem))    
       {    
          echo "file: $ourItem <br />";    
       }    
    }

closedir($ourDirList);

?>

This is to echo both folders and files in a Directory.

这是为了回显目录中的文件夹和文件。

#4


1  

glob function can be used to fetch directories too.

glob函数也可用于获取目录。

<?php
  $folders = glob('/',GLOB_ONLYDIR);
  print_r($folders);
?>

#1


1  

<?php
$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
        }
        closedir($dh);
    }
}
?>

credit to php.net

信用到php.net

Extending this logic,

扩展这个逻辑,

<?php
$dir = "/etc/php5/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if(filetype($dir . $file) == 'dir') {
                echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
            }
        }
        closedir($dh);
    }
}
?>

should echo all directories

应该回显所有目录

The output on my local server is:

我本地服务器上的输出是:

filename: . : filetype: dir
filename: .. : filetype: dir
filename: apache : filetype: dir
filename: etc : filetype: dir
filename: pranav : filetype: dir

#2


1  

Here is some example code from php.net on the readdir() function:

以下是来自php.net的readdir()函数的一些示例代码:

<?php
if ($handle = opendir('.')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            echo "$entry\n";
        }
    }
    closedir($handle);
}
?>

If you modify this code to check $entry with the is_dir() function, you can easily find your directories.

如果修改此代码以使用is_dir()函数检查$ entry,则可以轻松找到目录。

#3


1  

<?php       

    // declare the folder    
    $ourDir = "/home/public_html/folderName";

    // prepare to read directory contents    
    $ourDirList = @opendir($ourDir);

    // loop through the items    
    while ($ourItem = readdir($ourDirList))    
    {        
       // check if it is a directory    
       if (is_dir($ourItem))    
       {    
          echo "directory: $ourItem <br />";    
       }

       // check to see if it is a file    
       if (is_file($ourItem))    
       {    
          echo "file: $ourItem <br />";    
       }    
    }

closedir($ourDirList);

?>

This is to echo both folders and files in a Directory.

这是为了回显目录中的文件夹和文件。

#4


1  

glob function can be used to fetch directories too.

glob函数也可用于获取目录。

<?php
  $folders = glob('/',GLOB_ONLYDIR);
  print_r($folders);
?>