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

发布于 2024-07-27 05:32:17 字数 1144 浏览 2 评论 0原文

我不确定这有多简单,但我正在使用一个显示特定文件夹中的文件的脚本,但是我希望它们按字母顺序显示,这样做会很难吗? 这是我正在使用的代码:

if ($handle = opendir($mainframe->getCfg( 'absolute_path' ) ."/images/store/")) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..")  {
                if (($file != "index.html")&&($file != "index.php")&&($file != "Thumbs.db")) {
                $strExt = end(explode(".", $file));
                    if ($strExt == 'jpg') {
                        $Link = 'index.php?option=com_shop&task=deleteFile&file[]='.$file;
                        $thelist .= '<tr class="row0"><td nowrap="nowrap"><a href="'.$Link.'">'.$file.'</a></td>'."\n";
                        $thelist .= '<td align="center" class="order"><a href="'.$Link.'" title="delete"><img src="/administrator/images/publish_x.png" width="16" height="16" alt="delete"></a></td></tr>'."\n";
                    }

                }
            }
        }
        closedir($handle); 
    }   
    echo $thelist;

:)

I'm not sure how simple this would be, but I'm using a script which displays the files from a specific folder, however I'd like them to be displayed in alphabetical order, would it be hard to do this? Here's the code I'm using:

if ($handle = opendir($mainframe->getCfg( 'absolute_path' ) ."/images/store/")) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..")  {
                if (($file != "index.html")&&($file != "index.php")&&($file != "Thumbs.db")) {
                $strExt = end(explode(".", $file));
                    if ($strExt == 'jpg') {
                        $Link = 'index.php?option=com_shop&task=deleteFile&file[]='.$file;
                        $thelist .= '<tr class="row0"><td nowrap="nowrap"><a href="'.$Link.'">'.$file.'</a></td>'."\n";
                        $thelist .= '<td align="center" class="order"><a href="'.$Link.'" title="delete"><img src="/administrator/images/publish_x.png" width="16" height="16" alt="delete"></a></td></tr>'."\n";
                    }

                }
            }
        }
        closedir($handle); 
    }   
    echo $thelist;

:)

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

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

发布评论

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

评论(5

梦与时光遇 2024-08-03 05:32:17

您可以简单地使用 scandir文档),默认按字母顺序排序。

scandir 的返回值是一个数组而不是字符串,因此您的代码必须稍微调整,以迭代数组而不是检查最终的null 返回价值。 另外,scandir 采用带有目录路径的字符串而不是文件句柄作为输入,新版本将如下所示:

foreach(scandir($mainframe->getCfg( 'absolute_path' ) ."/images/store/") as $file) {
  // rest of the loop could remain unchanged
}

Instead of using readdir you could simply use scandir (documentation) which sorts alphabetically by default.

The return value of scandir is an array instead of a string, so your code would have to be adjusted slightly, to iterate over the array instead of checking for the final null return value. Also, scandir takes a string with the directory path instead of a file handle as input, the new version would look something like this:

foreach(scandir($mainframe->getCfg( 'absolute_path' ) ."/images/store/") as $file) {
  // rest of the loop could remain unchanged
}
星光不落少年眉 2024-08-03 05:32:17

该代码看起来相当混乱。 您可以将目录遍历逻辑与表示分离。 一个更简洁的版本(在我看来):

<?php

// Head of page
$it = new DirectoryIterator($mainframe->getCfg('absolute_path') . '/images/store/'));

foreach ($it as $file) {
    if (preg_match('#\.jpe?g$#', $file->getFilename()))
        $files[] = $file->getFilename();
}
sort($files);

// Further down
foreach ($files as $file)
    // display links to delete file.
?>

您甚至不需要担心打开或关闭句柄,并且由于您使用正则表达式检查文件名,因此不需要任何爆炸或条件检查。

That code looks pretty messy. You can separate the directory traversing logic with the presentation. A much more concise version (in my opinion):

<?php

// Head of page
$it = new DirectoryIterator($mainframe->getCfg('absolute_path') . '/images/store/'));

foreach ($it as $file) {
    if (preg_match('#\.jpe?g$#', $file->getFilename()))
        $files[] = $file->getFilename();
}
sort($files);

// Further down
foreach ($files as $file)
    // display links to delete file.
?>

You don't even need to worry about opening or closing the handle, and since you're checking the filename with a regular expression, you don't need any of the explode or conditional checks.

恰似旧人归 2024-08-03 05:32:17

我喜欢 Glob
它使目录读取变得轻而易举,因为它返回一个易于排序的数组:

<?php
$files = glob("*.txt");
sort($files);
foreach ($files as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}
?>

I like Glob
It makes directory reading a snap as it returns an array that's easily sortable:

<?php
$files = glob("*.txt");
sort($files);
foreach ($files as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}
?>
恬淡成诗 2024-08-03 05:32:17

如果您使用的是 Joomla1.5,则应该使用定义的常量 JPATH_BASE 而不是

$mainframe->getCfg( 'absolute_path' )

如果这是您要分发的 Joomla 扩展,请勿使用 scandir()因为它只是 PHP5。

最好的办法是使用 Joomla API。 它有一个用于目录和文件访问的类,这些类是分层的,可以通过不同的网络和协议来执行此操作。 因此,文件系统可以通过 FTP 等方式,并且可以针对任何网络/协议扩展类。

jimport( 'joomla.filesystem.folder' );
$files = JFolder::files(JPATH_BASE."/images/store/");
sort($files); 
foreach($files as $file) { 
   // do your filtering and other task
}

您还可以将正则表达式作为第二个参数传递给 JFolder::files() 来过滤您收到的文件。

您也不想使用像 /administrator/ 这样的 URL 文字,因为它们可以更改。
使用 JURI 方法,例如:

JURI::base();

如果您想确保表中的 Joomla CSS 类,请

'<tr class="row0">'

使用:

'<tr class="row'.($i&1).'">'

其中 $i 是迭代次数。 这将为您提供一系列交替的 0 和 1。

If you're using Joomla1.5 you should be using the defined constant JPATH_BASE instead of

$mainframe->getCfg( 'absolute_path' )

If this is a Joomla extension that you will distribute, don't use scandir() as it is PHP5 only.

The best thing to do is to use the Joomla API. It has a classes for directory and file access that is layered to do this over different networks and protocols. So the file system can be over FTP for example, and the classes can be extended for any network/protocol.

jimport( 'joomla.filesystem.folder' );
$files = JFolder::files(JPATH_BASE."/images/store/");
sort($files); 
foreach($files as $file) { 
   // do your filtering and other task
}

You can also pass a regular expression as the second parameter to JFolder::files() that filters the files you receive.

You also don't want to use URL literals like /administrator/ since they can be changed.
use the JURI methods like:

JURI::base();

If you want to make sure of the Joomla CSS classes in the tables, for:

'<tr class="row0">'

use:

'<tr class="row'.($i&1).'">'

where $i is the number of iterations. This gives you a sequence of alternating 0s and 1s.

顾挽 2024-08-03 05:32:17

如果我们有 PHP 内置函数,请始终使用它,它们会更快。
如果适合您的需要,请使用 glob 而不是遍历文件夹。

$folder_names = 数组();
$folder_names = glob( '*', GLOB_ONLYDIR + GLOB_MARK + GLOB_NOSORT );

  • 返回当前目录中的所有内容,在调用之前使用 chdir()
    删除 GLOB_ONLYDIR 以也包含文件( . 仅包含文件)
    GLOB_MARK 用于在文件夹名称中添加斜杠
    删除 GLOB_NOSORT 不对数组进行排序

if we have PHP built in functions, always use it, they are faster.
use glob instead of traversing folders, if it fits for your needs.

$folder_names = array();
$folder_names = glob( '*', GLOB_ONLYDIR + GLOB_MARK + GLOB_NOSORT );

  • returs everything in the current directory, use chdir() before calling it
    remove the GLOB_ONLYDIR to include files too ( . would be only files )
    GLOB_MARK is for adding a slash to folders names
    Remove GLOB_NOSORT not to sort the array
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文