2013-06-20 12 views
0

對於缺少更好的單詞,我將其他代碼的反編程替換爲更高效的代碼塊。到目前爲止,我已經完成了基本的PHP編程,以至於我之前沒有遇到的一些問題。包括以下內容;如何在php中使用readdir - 解釋別人的代碼

$handle = opendir("/directory/of/video/files/"); //not the actual directory 
$filename = readdir($handle); 

根據我的理解,readdir()只返回該目錄中的第一個文件。然而,PHP代碼的其餘部分設置好像它遍歷所有文件。

有什麼我誤解?

更新1 - 由DCoder

要求這裏是整個代碼(除bazillion if語句)的要求。如果視頻信息尚未存在,該程序的一般用途是將視頻信息上傳到數據庫。它需要查看目錄中的每個文件來檢查這個文件,但它只是從我的理解中看到一個文件。

<html> 
<body> 

<?php 

//opens the directory with symbolic links to the video files (automatically populated) 
$handle = opendir("/home/rgood/www/hpa2013fall/"); 
$filename = readdir($handle); 

if ($handle){ 
    while (false !== ($filename = readdir($handle))){ 
     if ($filename[0] == "v" && strlen($filename) == 25) 

     //retrieves date and time information variables from filename 
     $month = $filename[7] . $filename[8] . $filename[9]; 
     $day = $filename[10] . $filename[11]; 
     $hour = $filename[13] . $filename[14]; 
     $minute = $filename[16] . $filename[17]; 
     $second = $filename[19] . $filename[20]; 

/*a bazillion if statements to determine which file its accessed. 
This is the part I'm replacing with a database that I've created 
and can easily populate using an excel vba script and importing it to mysql*/ 
     if ($month == "February" && $day == "4") 
     {$title = "Title1"; $description = "Description1";} 
    elseif //... 
     //. 
     //. 
     //. 
     else 
     {$title = "No Title"; $description = "No description.";} 

     //open database connection 
     mysql_connect("server","user","password"); //proper connection settings in actual code 

     //select database 
     mysql_select_db("Database_Name"); //proper name in code 

     //this mysql checks to see if the filename already exists in the database 
     $ifquery = "SELECT * FROM Table WHERE Filename='$filename'"; 
     $ifresults = mysql_query($ifquery); 
     $rows = array(); 

     //this loop fetches the results. if the same filename is found, it populates $rows[] 
     while($row = mysql_fetch_array($ifresults)) 
     { 
      $rows[] = $row; 
     } 

      //adds an additional entry into the 
     if(empty($rows)) 
     { 
      //this is a readable form of the date/time and a concatenated description 
      $date = $month . ' ' . $day . ' 2013'; 
      $time = $hour . ':' . $minute . ':' . $second; 

      //the timedec will be a decimal representation of the date/time 
      //this is not an easily human-readable format, but it is easily 
      //used by mysql with the sort function 
      $timedec = "1" . $monthdec . $day . $hour . $minute . $second; 
      //sql code to insert the file into the table 
      $sql = "INSERT INTO Table(Filename, 
      Title, 
      Date, 
      Description, 
      Time, 
      timedec) 
      VALUES ('$filename', 
      '$title', 
      '$date', '$description', '$time', '$timedec')"; 
       mysql_query($sql) or die(mysql_error()); 
      } 
     } 
    } 
     closedir($handle); 
} 

?> 

</body> 
</html> 
+1

[RTFM](http://www.php.net/manual/en/function.readdir.php) – hek2mgl

+0

很難說沒有看到代碼的其餘部分... – DCoder

+0

@DCoder - 我已經更新了問題,並按照你的要求包含了其餘的代碼。 and @ hek2mgl - 謝謝,我已經看到那個頁面,但是,它表示「返回目錄中下一個條目的名稱,這些條目按文件系統存儲的順序返回」。這是令人困惑的,因爲它使用了一個和所有的答案來回答我的問題。 – Mason

回答

0

每次調用readdir都會得到另一個文件。

通過將readdir放在while()中,您可以讀取整個文件夾。

0

使用一個循環:

$dh = opendir('.'); 
while($file = readdir($dh)) { 
    do_something($file); 
} 

當沒有更多的文件檢索,READDIR()將返回一個布爾值false,導致循環終止。