按字母顺序列出文件(文件和文件夹)

发布于 2024-08-24 06:40:59 字数 410 浏览 4 评论 0原文

如何使用 PHP 按字母顺序列出文件夹中的所有文件和文件夹?

我对文件 a.txtb.txtcd.txt 使用了以下内容,其中c是一个文件夹。问题是 c 显示在最后,而不是在 b.txt 之后,因为它是一个文件夹。

我还希望能够检查每个文件是文件还是文件夹。

<?php
    $dir = opendir ("folders");
    while (false !== ($file = readdir($dir))) {
        echo "$file <br />";
    }
?>

How would I list all the files and folders in a folder alphabetically with PHP?

I used the following for the files a.txt, b.txt, c, and d.txt, Where c is a folder. The problem is that c is displayed last instead of after b.txt because it is a folder.

I'd also like to be able to check if each is either a file or folder.

<?php
    $dir = opendir ("folders");
    while (false !== ($file = readdir($dir))) {
        echo "$file <br />";
    }
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

盗心人 2024-08-31 06:40:59

glob() 的强大功能可以为您提供帮助。只要这样做:

$dir = glob("folders/*");

The power of glob() is here to help you. Just do:

$dir = glob("folders/*");
想你只要分分秒秒 2024-08-31 06:40:59

只需先将名称读入数组,而不是立即打印。然后对数组进行排序,然后进行输出。

<?php
$dir = opendir ("folders");
while (false !== ($file = readdir($dir))) {
    $names[] = $file;
}
sort($names, SORT_STRING);
foreach ($names as $name) {
    echo "$name <br />";
}
?>

Just read the names into an array first instead of printing immediately. Then sort the array, and then do your output.

<?php
$dir = opendir ("folders");
while (false !== ($file = readdir($dir))) {
    $names[] = $file;
}
sort($names, SORT_STRING);
foreach ($names as $name) {
    echo "$name <br />";
}
?>
醉梦枕江山 2024-08-31 06:40:59
$files = scandir('folders');
sort($files);
foreach ($files as $file) {
    echo $file.'<br />';
}
$files = scandir('folders');
sort($files);
foreach ($files as $file) {
    echo $file.'<br />';
}
冰葑 2024-08-31 06:40:59

我建议使用以下代码(不需要 opendir 等)

$entries = glob("*");
sort($entries); // This is optional depending on your os, on linux it works the way you want w/o the sort
var_dump($entries);

/* Output
array(4) {
  [0]=>
  string(5) "a.txt"
  [1]=>
  string(5) "b.txt"
  [2]=>
  string(1) "c"
  [3]=>
  string(5) "d.txt"
}
*/

对于您问题的第二部分:您使用 php“is_file”和“is_dir”函数

I would suggest the following code (no need for opendir etc.)

$entries = glob("*");
sort($entries); // This is optional depending on your os, on linux it works the way you want w/o the sort
var_dump($entries);

/* Output
array(4) {
  [0]=>
  string(5) "a.txt"
  [1]=>
  string(5) "b.txt"
  [2]=>
  string(1) "c"
  [3]=>
  string(5) "d.txt"
}
*/

For the secound part of your question: You the php "is_file" and "is_dir" functions

捎一片雪花 2024-08-31 06:40:59

只需先将名称读入数组,而不是立即打印。然后对数组进行排序,然后进行输出。

<?php
    $files = array();
    $dir = opendir ("folders");
    while (false !== ($file = readdir($dir))) {
        $files[] = $file;
    }
    sort($files);

    foreach ($files as $f)
        echo "$f <br />";
?>

Just read the names into an array first instead of printing immediately. Then sort the array, and then do your output.

<?php
    $files = array();
    $dir = opendir ("folders");
    while (false !== ($file = readdir($dir))) {
        $files[] = $file;
    }
    sort($files);

    foreach ($files as $f)
        echo "$f <br />";
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文