0

由于缺乏更好的词,我正在对其他人的代码进行反向编程,以便用更有效的代码块替换其中的一部分。到目前为止,我主要在 php 中完成了基本的编程,所以有些东西我以前没有遇到过。包括以下内容;

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

据我了解 readdir() 只返回该目录中的第一个文件。然而,其余 php 代码的设置就像遍历所有文件一样。

我有什么误解吗?

更新 1 - 由 DCoder 请求

这是所要求的整个代码(除了大量的 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>
4

2 回答 2

0

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

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

于 2013-06-20T17:06:03.223 回答
0

使用循环:

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

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

于 2013-06-20T17:14:19.063 回答