如何在此代码中将while循环转换为foreach?

时间:2014-09-26 09:48:45

标签: php php-5.3

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Video display</title>
</head>

<body>
<?php
    $directory="video/";
    if(is_dir($directory))
    {
        if($openfile=(opendir($directory)))
        {
            while(($video=readdir($openfile))!==false)
            {
                if($video!="." && $video!="..")
                {
                ?>
                    <video controls autoplay>
                        <source src="video/<?php echo $video;?>"/>
                    </video>
                <?php
                    echo $video;
                }
            }
        }
        closedir($openfile);
    }
?>
</body>
</html>

此代码运行正常,但我想在 foreach循环中显示视频,以便如何在此代码中将while循环转换为 foreach循环

1 个答案:

答案 0 :(得分:1)

您可以使用glob

<?php

foreach( glob("videos/*") as $strFileName) {
    echo "<video controls autoplay>
            <source src=\"video/". $strFileName ."\"/>
          </video>";
}