2018-05-12 14:27
浏览 66


I'm trying to display all videos a webpage using the following code, I've gotten as far as being able to iterate over the files, printing the file names and embedding a video. However the videos are greyed out and don't work, I suspect I did something wrong with using $filename in the code.

$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $filename) {
    if (!$filename->isDot()) {

        if ($filename != "index.php" and $filename != "error_log") {

            echo $filename, "<br>"; 

            echo '<video width="400" controls="controls" preload="metadata">
            <source src=$filename type="video/mp4"></video>';

            echo "<br><br>";


This is how it shows up:

enter image description here

enter image description here

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用以下代码在网页上显示所有视频,我已经尽力了 迭代文件,打印文件名和嵌入视频。 然而,视频是灰色的,不起作用,我怀疑我在代码中使用$ filename做错了。

 $ dir = new  DirectoryIterator(dirname(__ FILE __)); 
foreach($ dir as $ filename){
 if(!$ filename-&gt; isDot()){
 if if($ filename!=“index.php”和$  filename!=“error_log”){
 echo $ filename,“&lt; br&gt;”;  
 echo'&lt; video width =“400”controls =“controls”preload =“metadata”&gt; 
&lt; source src = $ filename type =“video / mp4”&gt;&lt; / video&gt;'  ; 
 echo“&lt; br&gt;&lt; br&gt;”; 


< / p>

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • droxy80248 2018-05-12 14:31


    You are echo $filename like a string, not a PHP variable.

    Remember, if echo with single quotes ' all inside it will echo like string and echo with double quotes " will echo PHP variables reading their value. Informations about strings in PHP are nicley explained e.g. here .

    You can change your piece of code witch echo HTML video to:

    echo '<video width="400" controls="controls" preload="metadata"><source src="' . $filename . '" type="video/mp4"></video>';

    Note that there were also missing double quotes for HTML video tag src property and I added them in code above.


    点赞 评论
  • du8864 2018-05-12 14:31


    <source src=$filename type="video/mp4"></video>';


    <source src="'.$filename.'" type="video/mp4"></video>';
    点赞 评论
  • douyu5775 2018-05-12 15:02

    Your php variable is inside single quotes and (from the doc) variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

    As an alternative you could keep your string between single quotes and make use of sprintf which will return a formatted string and use for example %s to specify that the argument should be handled as string type.

    echo sprintf('<video width="400" controls="controls" preload="metadata">
    <source src="%s" type="video/mp4"></video>',
    点赞 评论

相关推荐 更多相似问题