Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP(文件夹)文件是否按字母顺序列出?_Php_File_Directory_Alphabetized - Fatal编程技术网

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

PHP(文件夹)文件是否按字母顺序列出?,php,file,directory,alphabetized,Php,File,Directory,Alphabetized,我不确定这会有多简单,但我使用的脚本显示特定文件夹中的文件,但是我希望它们按字母顺序显示,这样做会很困难吗?以下是我使用的代码: if ($handle = opendir($mainframe->getCfg( 'absolute_path' ) ."/images/store/")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file

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

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;
if($handle=opendir($mainframe->getCfg('absolute_path'))。“/images/store/”){
while(false!=($file=readdir($handle))){
如果($file!=“&&&$file!=”){
如果(($file!=“index.html”)&&($file!=“index.php”)&&($file!=“Thumbs.db”)){
$strExt=end(分解(“.”,$file));
如果($strExt=='jpg'){
$Link='index.php?option=com_shop&;task=deleteFile&;file[]='。$file;
$thelist.=''。\n“;
$thelist.=''。\n“;
}
}
}
}
closedir($handle);
}   
echo$thelist;

:)

不用
readdir
您只需使用默认按字母顺序排序的
scandir
()

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

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

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

我喜欢
它使目录读取成为一个快照,因为它返回一个易于排序的数组:

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

如果使用Joomla1.5,则应使用定义的常量JPATH\u BASE而不是

$mainframe->getCfg( 'absolute_path' )
如果这是您将分发的Joomla扩展,请不要使用scandir(),因为它只是PHP5

最好的方法是使用JoomlaAPI。它有一个目录和文件访问类,可以通过不同的网络和协议分层访问。例如,文件系统可以通过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">'
“”
使用:

“”

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

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

$folder_names=array(); $folder_names=glob('*',glob_ONLYDIR+glob_MARK+glob_NOSORT)

  • 在调用当前目录中的所有内容之前,请使用chdir() 删除GLOB_ONLYDIR以包含文件(.a将是唯一的文件) GLOB_标记用于在文件夹名称中添加斜杠 删除GLOB_NOSORT以不对数组排序

不总是这样。如果您有一个标准化的API,请使用它。在本例中,代码是为Joomla CMS编写的。因此,它应该使用Joomla API。在web开发中,速度很少受到关注。好的设计是。在试图解决同样的问题时找到了这个答案。谢谢工作良好,代码更少!这很有效。阅读文档后,我甚至可以使用
SCANDIR\u SORT\u DESCENDING
作为
SCANDIR(…)
的第二个参数来反转排序顺序。在提供的链接中还记录了其他几个排序常量。
'<tr class="row0">'
'<tr class="row'.($i&1).'">'