通过PHP访问不同文件夹中的视频

通过PHP访问不同文件夹中的视频,php,video,Php,Video,我对PHP非常陌生,但我了解基础知识。我的问题是,我有一个索引页,需要有一个存储在不同文件夹中的视频名称列表。因为到网站上线时,文件夹中会有100多个视频,所以我希望视频名称可以点击,点击后视频会播放 <?php $videopath = 'videos'; $videoExts = array('webm'=> 'video/webm','mp4'=>'video/mp4','mpeg'=>'video/mp4','ogv'=>'video/ogv'); $dir

我对PHP非常陌生,但我了解基础知识。我的问题是,我有一个索引页,需要有一个存储在不同文件夹中的视频名称列表。因为到网站上线时,文件夹中会有100多个视频,所以我希望视频名称可以点击,点击后视频会播放

<?php
$videopath = 'videos';
$videoExts = array('webm'=> 'video/webm','mp4'=>'video/mp4','mpeg'=>'video/mp4','ogv'=>'video/ogv');
$directory = "/videos";
$phpfiles = glob($directory . "*.html");

if ($handle = opendir($videopath)) {
    while (false !== ($file = readdir($handle))) {
        $info =pathinfo($file);
        $ext = strtolower($info['extension']);
        echo $file . " \n " . "\n <br> \n";

        if (array_key_exists($ext, $videoExts)) {      
?>
   <div class="flowplayer" data-swf="flowplayer.swf" data-ratio="0.4167">
      <video>
         <source type="<?php echo $videoExts[$ext]; ?>" src="<?php echo "$videopath/$file"; ?>">
      </video>
   </div>

<?php
        }
    }
    closedir($handle);
} else {
    echo "no dir";
}

?>

替换:

   <div class="flowplayer" data-swf="flowplayer.swf" data-ratio="0.4167">
      <video>
         <source type="<?php echo $videoExts[$ext]; ?>" src="<?php echo "$videopath/$file"; ?>">
      </video>
   </div>

我将执行以下操作(它涉及一点jQuery)。这个想法是要有一个视频列表和一个视频播放器。单击其中一个后,视频播放器将更新

步骤:

  • 列出视频链接,使用HTML5数据属性存储每个视频的源和扩展信息。因此,单击时,唯一的视频播放器将使用新设置进行更新

    $videopath='videos';
    $videoExts=array('webm'=>'video/webm','mp4'=>'video/mp4','mpeg'=>'video/mp4','ogv'=>'video/ogv');
    $directory=“/videos”;
    $phpfiles=glob($directory.*.html”);
    //存储列表中的第一个视频
    $first_video=array();
    如果($handle=opendir($videopath)){
    $counter=0;
    while(false!=($file=readdir($handle))){
    $info=pathinfo($file);
    $ext=strtolower($info['extension']);
    echo$文件。“\n”。“\n
    \n”; 如果(array_key_存在($ext,$videoExts)){ 如果($counter==1){ //保存第一个视频的设置 $first_vid['extension']=$videoExts[$ext]; $first_vid['path']=$videopath./'.$file; //保存在数组中 $first_video[]=$first_vid; } ?>

    感谢您的帮助。在应用这些更改后,除了page2.php上的视频文件外,其他一切都正常工作。我将调整您的代码,看看是否可以正常工作。实际上,在单击周围后,视频可以播放,但控件不会自动弹出。再次感谢您@zeflex t的帮助不过,我已经实现了@zeflex的代码,它对我来说非常有用。
    <a href="page2.php?type=<?php echo $videoExts[$ext]; ?>&src=<?php echo "$videopath/$file"; ?>" target="_blank"><?php echo $file; ?></a>
    
    <?php 
    $type = $_GET['type'];
    $src = $_GET['src'];
    if (file_exists($src)) { ?>
                <div class="flowplayer" data-swf="flowplayer.swf" data-ratio="0.4167">
                      <video>
                         <source type="<?php echo $type; ?>" src="<?php echo $src; ?>">
                      </video>
                   </div>
            <?php } ?>
    
    $videopath = 'videos';
    $videoExts = array('webm'=> 'video/webm','mp4'=>'video/mp4','mpeg'=>'video/mp4','ogv'=>'video/ogv');
    $directory = "/videos";
    $phpfiles = glob($directory . "*.html");
    
    // Stores first video of the list
    $first_video = array();
    
    if ($handle = opendir($videopath)) {
    
        $counter = 0;
    
        while (false !== ($file = readdir($handle))) {
            $info =pathinfo($file);
            $ext = strtolower($info['extension']);
            echo $file . " \n " . "\n <br> \n";
    
            if (array_key_exists($ext, $videoExts)) {      
              if ($counter == 1) {
    
                // Save settings of first video
                $first_vid['extension'] = $videoExts[$ext];
                $first_vid['path'] = $videopath .'/'. $file;
    
                // Save it in array
                $first_video[] = $first_vid;
              }
    ?>
    
        <a class="video-item" href="#" data-type="<?php echo $videoExts[$ext]; ?>" data-src="<?php echo "$videopath/$file"; ?>"><?php echo "$videopath/$file"; ?></a><br/>
    
    <?php
            }
        }
        closedir($handle);
    } else {
        echo "no dir";
    }
    
    ?>
    
    <div class="flowplayer" data-swf="flowplayer.swf" data-ratio="0.4167">
      <video>
         <source type="<?php echo $first_video[0]['extension']; ?>" src="<?php echo $first_video[0]['path']; ?>">
      </video>
    </div>
    
    
    <script>
    // Don't forget to add jQuery in your project for this to work :)        
    
    $(function () {
      $('.video-item').click(function (e) {
        // do nothing
        e.preventDefault();
    
        // get info of video being clicked
        var extension = $(this).attr("data-type");
        var src = $(this).attr("data-src");
    
        // update video player with clicked video
        $('.flowplayer video').attr('type', extension);
        $('.flowplayer video').attr('src', src);
      });
    });
    
    
    </script>