2016-09-17 133 views
0

我想按字母順序排列我的文件,但我不知道該怎麼辦。我的意思是,我希望這些目錄將成爲ABC訂單的第一個目錄,以及ABC訂購的文件之後。 我嘗試了幾個選項,但我無法做到這一點。 這裏是我的代碼:按字母順序排列並顯示目錄列表PHP

<?php 

    $dirlist = getFileList("./members/files/");?> 

    <table border="1"> 
     <thead> 
      <tr> 
       <th>Name</th> 
       <th>Type</th> 
       <th>Size</th> 
       <th>Last Modified</th> 
      </tr> 
     </thead> 
     <tbody> 
      <?php 
      // output file list as table rows 
      foreach($dirlist as $file) { 
       echo "<tr>\n"; 

        $file['nams'] = basename($file['name']); 
        $file['nams2'] = pathinfo($file['nams'], PATHINFO_EXTENSION); 
        $file['nams'] = pathinfo($file['nams'], PATHINFO_FILENAME); 

        if($file['nams2'] != 'mp4' && $file['nams2'] != '') 
        { 
         echo "<td><a href=\"/members/get_file.php?name={$file['nams']}&ext={$file['nams2']}\">",basename($file['name']),"</a></td>\n"; 
        } else if($file['nams2'] != '') { 
         echo "<td><a href=\"/video.php?name={$file['nams']}\">",basename($file['name']),"</a></td>\n"; 
        } else { 
         echo "<td><a href=\"#\">",basename($file['name']),"</a></td>\n"; 
        } 

        echo "<td>{$file['type']}</td>\n"; 
        echo "<td>{$file['size']}</td>\n"; 
        echo "<td>",date('Y-m-d H:i', $file['lastmod']),"</td>\n"; 

       echo "</tr>\n"; 
      } 
      ?> 
     </tbody> 
    </table> 

    <?php 
    function getFileList($dir, $recurse=false, $depth=false) 
    { 
     $retval = array(); 

     // add trailing slash if missing 
     if(substr($dir, -1) != "/") $dir .= "/"; 

     // open pointer to directory and read list of files 
     $d = @dir($dir) or die("getFileList: Failed opening directory $dir for reading"); 
     while(false !== ($entry = $d->read())) { 
      // skip hidden files 
      if($entry[0] == ".") continue; 
      if(is_dir("$dir$entry")) { 
       $retval[] = array(
        "name" => "$dir$entry/", 
        "type" => filetype("$dir$entry"), 
        "size" => 0, 
        "lastmod" => filemtime("$dir$entry") 
       ); 
       if($recurse && is_readable("$dir$entry/")) { 
        if($depth === false) { 
         $retval = array_merge($retval, getFileList("$dir$entry/", true)); 
        } elseif($depth > 0) { 
         $retval = array_merge($retval, getFileList("$dir$entry/", true, $depth-1)); 
        } 
       } 
      } elseif(is_readable("$dir$entry")) { 
       $retval[] = array(
        "name" => "$dir$entry", 
        "type" => mime_content_type("$dir$entry"), 
        "size" => formatSizeUnits(filesize("$dir$entry")), 
        "lastmod" => filemtime("$dir$entry") 
       ); 
      } 
     } 
     $d->close(); 

     return $retval; 
    } 

    function formatSizeUnits($bytes) 
    { 
     if ($bytes >= 1073741824) 
     { 
      $bytes = number_format($bytes/1073741824, 2) . ' GB'; 
     } 
     elseif ($bytes >= 1048576) 
     { 
      $bytes = number_format($bytes/1048576, 2) . ' MB'; 
     } 
     elseif ($bytes >= 1024) 
     { 
      $bytes = number_format($bytes/1024, 2) . ' kB'; 
     } 
     elseif ($bytes > 1) 
     { 
      $bytes = $bytes . ' bytes'; 
     } 
     elseif ($bytes == 1) 
     { 
      $bytes = $bytes . ' byte'; 
     } 
     else 
     { 
      $bytes = '0 bytes'; 
     } 

     return $bytes; 
    } 
?> 

回答

0

你可以使用scandir到一個數組的形式返回排序的文件列表。

例子:類似於

<?php 
    $directory_list = scandir("./members/files/"); 
    print_r($directory_list); 

這將輸出目錄列表:

Array 
(
    [0] => . 
    [1] => .. 
    [2] => bar.php 
    [3] => foo.txt 
    [4] => somedir 
    ... 
) 

你可以像下面這樣做對他們進行降序排列:

print_r($directory_list, SCANDIR_SORT_DESCENDING); 

你可以找到更多關於scandirPHP documentation