简体   繁体   English

使用PHP变量的HTML嵌入视频不起作用

[英]HTML embed video using PHP variable doesn't work

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. 但是这些视频显示为灰色,无法正常工作,我怀疑我在代码中使用$ filename做错了。

<?php
$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: 它是这样显示的:

在此处输入图片说明

在此处输入图片说明

Hi 你好

You are echo $filename like a string, not a PHP variable. 您像字符串一样回显$filename ,而不是PHP变量。

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 eg here . 请记住,如果用单引号呼应'所有在它里面会响应像串并用双引号回声"将呼应PHP变量阅读他们的价值。信息有关PHP字符串nicley解释如这里

You can change your piece of code witch echo HTML video to: 您可以将您的代码片段echo HTML视频更改为:

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. 请注意,HTML video标签src属性也缺少双引号,我在上面的代码中将它们添加了。

Cheers 干杯

Change 更改

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

to

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

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. 您的php变量位于单引号内,并且(来自doc)变量和特殊字符的转义序列在单引号引起来的字符串中不会扩展。

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. 作为替代方案,您可以将字符串保留在单引号之间,并使用sprintf ,它将返回格式化的字符串,并使用例如%s来指定应将参数作为字符串类型进行处理。

echo sprintf('<video width="400" controls="controls" preload="metadata">
<source src="%s" type="video/mp4"></video>',
    $filename);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM