0

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.

<?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:

enter image description here

enter image description here

2
  • src=$filename should be ' . $filename . ' ? Commented May 12, 2018 at 14:29
  • use double quotes instead: echo "<video width='400' controls='controls' preload='metadata'><source src=$filename type='video/mp4'></video>"; Commented May 12, 2018 at 14:32

3 Answers 3

2

Hi

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.

Cheers

Sign up to request clarification or add additional context in comments.

Comments

1

Change

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

to

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

Comments

1

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>',
    $filename);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.