如何在php中使用readdir - 解释别人的代码

时间:2013-06-20 17:03:30

标签: php

由于缺乏一个更好的词,我反向编程别人的代码,以便用更有效的代码块替换它的一部分。到目前为止,我主要完成了基本的PHP编程,所以我之前没遇到过的一些东西。包括以下内容;

$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>

2 个答案:

答案 0 :(得分:0)

每次调用readdir都会获得另一个文件。

通过将readdir放入while(),您可以阅读整个文件夹。

答案 1 :(得分:0)

使用循环:

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

当没有更多要检索的文件时,readdir()将返回一个布尔值false,导致循环终止。