Question

I am making a website which will stream a selected video. I could make it show the file list. What I want is, when I select a file from list, it pastes filename into source link for video streaming. I cannot get this file name from the list that I got. Can anyone help me, please? Don't be mad, I'm just a beginner in this.

<form action="" method="post">
<?php

//scans files and sorts by date

$files = glob('/media/*.MP4');
usort($files, function($a, $b) {
    return filemtime($a) < filemtime($b);
});

// outputs a list of files, sorted by name

foreach($files as $file){
    printf('<p><tr><td><input type="radio" id="%1$s" name="box"></td>
            <td><label for="%1$s">%1$s</td></label>
            <td><label for="%1$s">%2$s</td></label></tr></p>', 
            basename($file), date('F d Y, H:i:s', filemtime($file)));
}

?>

<input type="submit" name="submit1" value="Submit"><INPUT type="reset">
</form>

<video width="320" height="240" controls>
  <source src="/media/<?php printf($_POST); ?>" type="video/mp4">  <--- I want to put the file name in this line

</video>
Was it helpful?

Solution

According to this line:

printf('<p><tr><td><input type="radio" id="%1$s" name="box"></td>
        <td><label for="%1$s">%1$s</td></label>
        <td><label for="%1$s">%2$s</td></label></tr></p>', 
        basename($file), date('F d Y, H:i:s', filemtime($file)));

Your input name is 'box'. You can access it after the form submission via $_POST['box'], something along the lines of:

<?php if(isset($_POST['box'])) { // This will hide the video prior to submission ?>
    <video width="320" height="240" controls>
      <source src="/media/<?php echo $_POST['box']; ?>" type="video/mp4"> 
    </video>
<?php } ?>

Update

Admittedly I should have seen this sooner, but your input name is being repeated many times. You don't want to have two elements in the same form that share the same name. You need to differentiate it them somehow, and in general you can achieve having many elements with the same name by simply making it an array:

printf('<p><tr><td><input type="radio" id="%1$s" name="box[]"></td>

Now you should be able to see an array when you submit and var_dump( $_POST['box'] )

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top