【问题标题】:endless directory list php无尽的目录列表php
【发布时间】:2015-11-18 10:06:55
【问题描述】:

我有这个 php 代码来列出目录中的所有文件并输出文件大小和下载链接。

<?
function human_filesize($bytes, $decimals = 2) {
$size = array(' B',' kB',' MB',' GB',' TB',' PB',' EB',' ZB',' YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}

$excludedFiles = array('.','..');

$excludedExtensions = array ('html','htm','php');

// Convert to lower case so we are not case-sensitive
for ($i = 0; isset($excludedFiles[$i]); $i++) $excludedFiles[$i] = strtolower(ltrim($excludedFiles[$i],'.'));
for ($i = 0; isset($excludedExtensions[$i]); $i++) $excludedExtensions[$i] = strtolower($excludedExtensions[$i]);


// Define the full path to your folder from root
$dir = "./";

// Open the folder
$dir_handle = @opendir($dir) or die("Unable to open $dir");

// Loop through the files
while ($file = readdir($dir_handle)) {

$extn = explode('.',$file);
$extn = array_pop($extn);

if (!in_array(strtolower($file),$excludedFiles) && !in_array(strtolower($extn),$excludedExtensions)){

if($file == "." || $file == ".." )


continue;

echo "<tr>
      <td>
     <a class='Testo' href=\"$file\" download>$file</a></td>
     <td><font class='TestoPiccoloBo'>[" . human_filesize(filesize($file)) . "]</font></td>
    </tr>";
 }
 }

// Close
closedir($dir_handle);



?>

我希望列表按字母顺序排列,所以我添加了

$files = scandir($dir);

$dir 行之后和

foreach ($files as $file){

while ($file = readdir($dir_handle)) { 行之后

还有一个

} 

closedir($dir_handle); 行之前

现在文件列表按字母顺序排列,但列表是无穷无尽的。列表一遍又一遍地开始,就像一个循环。 我究竟做错了什么?这是完成此任务的正确方法吗? 任何帮助将非常感激。 谢谢!

【问题讨论】:

    标签: php alphabetical directory-listing


    【解决方案1】:

    您可以将您的文件夹放在一个数组中,并使用 php 排序功能对其进行排序。然后打印它们:

    <?
    function human_filesize($bytes, $decimals = 2) {
    $size = array(' B',' kB',' MB',' GB',' TB',' PB',' EB',' ZB',' YB');
    $factor = floor((strlen($bytes) - 1) / 3);
    return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
    }
    
    $excludedFiles = array('.','..');
    $arrayFiles = array();
    $excludedExtensions = array ('html','htm','php');
    
    // Convert to lower case so we are not case-sensitive
    for ($i = 0; isset($excludedFiles[$i]); $i++) $excludedFiles[$i] = strtolower(ltrim($excludedFiles[$i],'.'));
    for ($i = 0; isset($excludedExtensions[$i]); $i++) $excludedExtensions[$i] = strtolower($excludedExtensions[$i]);
    
    
    // Define the full path to your folder from root
    $dir = "./";
    
    // Open the folder
    $dir_handle = @opendir($dir) or die("Unable to open $dir");
    
    // Loop through the files
    while ($file = readdir($dir_handle)) {
    
    $extn = explode('.',$file);
    $extn = array_pop($extn);
    
    if (!in_array(strtolower($file),$excludedFiles) && !in_array(strtolower($extn),$excludedExtensions)){
    
    if($file == "." || $file == ".." )
    continue;
    
    $arrayFiles[] =  $file;
    
     }
     } // dunno what are these } so i put my loop after
    
    // Close
    closedir($dir_handle);
    
    sort($arrayFiles);
    
    foreach ($arrayFiles as $file) {
        echo "<tr>
          <td>
         <a class='Testo' href=\"$file\" download>$file</a></td>
         <td><font class='TestoPiccoloBo'>[" . human_filesize(filesize($file)) . "]</font></td>
        </tr>";
    }
    ?>
    

    【讨论】:

    • 效果很好,谢谢!我刚刚在arrayFiles[] = $file; 之前添加了$
    • 太棒了!并感谢您对我的错字问题的回报:) 它已被编辑。
    猜你喜欢
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    相关资源
    最近更新 更多