0

より適切な言葉がないため、他の誰かのコードをリバース プログラミングして、その一部をより効率的なコード ブロックに置き換えています。これまで、私は主に php で基本的なプログラミングを行ってきたので、これまで出会ったことのないものもいくつかありました。以下を含む;

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

私の理解では、 readdir() はそのディレクトリの最初のファイルのみを返します。ただし、php コードの残りの部分は、すべてのファイルを通過するかのように設定されています。

私が誤解していることはありますか?

アップデート 1 - DCoder によるリクエスト

要求されたコード全体 (膨大な数の if ステートメントを除く) を次に示します。プログラムの一般的な目的は、ビデオ情報がまだデータベースにない場合に、ビデオ情報をデータベースにアップロードすることです。これを確認するには、ディレクトリ内の各ファイルを確認する必要がありますが、私の理解では 1 つしか確認できません。

<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 に答える