2017-06-25 79 views
0

這是使用的代碼IM:簡單的家庭流媒體服務器的PHP漢語拼音

<?php 

     $dir = "House Of Cards/"; 
     $videoW = 320; 
     $videoH = 240; 

     if (is_dir($dir)) 
     { 
      if ($dh = opendir($dir)){ 

       while (($file = readdir($dh)) !== false){ 

        if($file != '.' && $file != '..'){ 

         echo " 
         <div style='display: block'> 
         <a href= \"$dir/$file\">Watch \"$file\"</a> 
         </div> 
         "; 


        } 

       } 

       closedir($dh); 

       } 
     }; 
     ?> 

IM嘗試承載與PHP的一個小小的簡單的HTTP服務器在我的路由器等等,而我的工作

我可以流視頻到我的手機

基本上,我有一個index.php文件與具有視頻

能正常工作的每個文件夾,當視頻進行編碼權,但是當它列出文件夾中的錄像對方不「按字母順序排列」或非順序

他們拿出這樣的:

Watch "House Of Cards S01E01.mp4" 
Watch "House Of Cards S01E08.mp4" 
Watch "House Of Cards S01E05.mp4" 
Watch "House Of Cards S01E11.mp4" 
Watch "House Of Cards S01E03.mp4" 
Watch "House Of Cards S01E10.mp4" 
Watch "House Of Cards S01E02.mp4" 
Watch "House Of Cards S01E07.mp4" 
Watch "House Of Cards S01E09.mp4" 
Watch "House Of Cards S01E13.mp4" 
Watch "House Of Cards S01E12.mp4" 
Watch "House Of Cards S01E04.mp4" 
Watch "House Of Cards S01E06.mp4" 

任何人知道我可以使此代碼露面asequential或「按字母順序」列表?

回答

0

你應該看看這裏:natsort

本質上講,這功能將使用算法來在「自然」爲了您的文件進行排序,而不是按字母順序排列,因爲它是現在。

要添加到您的代碼中,您可以在循環目錄中的文件時將所有文件添加到數組中,並在結束每個文件的鏈接之前運行natsort。

<?php 

    $dir = "House Of Cards/"; 
    $videoW = 320; 
    $videoH = 240; 

    $files = []; // Initialize empty array 

    if (is_dir($dir)) { 
     if ($dh = opendir($dir)) { 
      while (($file = readdir($dh)) !== false) { 
       if($file != '.' && $file != '..') { 
        $files[] = $file; // add this file to array 
       } 
      } 
      closedir($dh); 
     } 
    } 

    natsort($files); // naturally sort files 

    // make a link for each file... 
    foreach ($files as $file) { 
     echo "<div style='display: block'> 
      <a href= \"$dir/$file\">Watch \"$file\"</a> 
      </div>"; 
    } 

希望這有助於!

+1

這工作完美。幾乎沒有修改。 (theres一個額外的「}」ID upvote這個答案,如果我可以 –

+0

哦,我的壞,刪除額外的「}」:) –

0
 if (is_dir($dir)) 
     { 
      if ($dh = opendir($dir)){ 

       while (($file = readdir($dh)) !== false){ 

        if($file != '.' && $file != '..'){ 
          $files[] = $file; 
        } 

       } 

       closedir($dh); 

       } 
     }; 

    $arrat_order[] = natsort($files); 
    foreach($arrat_order as $value){ 
        echo " 
         <div style='display: block'> 
         <a href= \"$dir/$value\">Watch \"$value\"</a> 
         </div> 
         "; 

} 
+0

請注意,natsort()將返回一個布爾值,而不是一個數組。檢查在這裏的PHP文檔:http://php.net/manual/en/function.natsort.php –