猿问

从文件夹中随机播放视频文件,递归地向下查看所有目录结构树

一些 JavaScript、JQuery 或 PHP 会递归地查看文件夹,向下查看所有目录结构树,找到新的视频文件并将其加载到 html5 视频标签源中,以便每次重新加载页面时自动播放。一个加号,当完成播放时,随机无缝地跳转到另一个视频文件。


这将适用于一个选定的文件夹,但不适用于所有目录结构树


<?php

$myVideoDir = '.';

$extension = 'mp4';

$videoFile = false;

$pseudoDir = scandir($myVideoDir);

$myitems = array();

$mycounter = 0;

foreach($pseudoDir as $item) {

    if ( $item != '..' && $item != '.' && !is_dir($item) ) {

        $ext = preg_replace('#^.*\.([a-zA-Z0-9]+)$#', '$1', $item);

        if ( $ext == $extension )

            $videoFile = $item;

            if ( $videoFile <> "" ) {

                $myitems[] = $videoFile;

                $mycounter = $mycounter + 1;

            }               

    }

}


$myrandom = rand(0,$mycounter-1);

if ( !!$videoFile ) {


    echo '<video id="dep" class="center" width="400" autoplay controls>        

            <source src="'.$myVideoDir.'/'.$myitems[$myrandom].'" type="video/mp4"> 

        </video>

    ';

}

?>


青春有我
浏览 158回答 1
1回答

繁华开满天机

我建议使用以下代码。<?php$myVideoDir = '.';$extension = 'mp4';$videoFile = false;$pseudoDir = scandir($myVideoDir);$myitems = array();foreach($pseudoDir as $item) {&nbsp; if ( $item != '..' && $item != '.' && !is_dir($item) ) {&nbsp; &nbsp; $ext = preg_replace('#^.*\.([a-zA-Z0-9]+)$#', '$1', $item);&nbsp; &nbsp; if ( $ext == $extension ) {&nbsp; &nbsp; &nbsp; $videoFile = $item;&nbsp; &nbsp; &nbsp; if ( $videoFile <> "" ) {&nbsp; &nbsp; &nbsp; &nbsp; array_push($myitems, $videoFile);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; }}$myrandom = rand(0,count($myitems)-1);if ( !!$videoFile ) {&nbsp; echo '<video id="dep" class="center" width="400" autoplay controls><source src="'.$myVideoDir.'/'.$myitems[$myrandom].'" type="video/mp4"></video>';}?>更新考虑做一个函数。<?phpfunction getFileList($dirPath, $ext){&nbsp; $list = scandir($dirPath);&nbsp; $fileList = array();&nbsp; foreach($list as $item) {&nbsp; &nbsp; if ($item != '..' && $item != '.' && !is_dir($item)) {&nbsp; &nbsp; &nbsp; $info = pathinfo($item);&nbsp; &nbsp; &nbsp; $videoFile = $item;&nbsp; &nbsp; &nbsp; if ($info['extension'] == $ext) {&nbsp; &nbsp; &nbsp; &nbsp; array_push($fileList, $item);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }&nbsp; }&nbsp; return $fileList;}function pickRandVid($l){&nbsp; $r = rand(0, count($l) - 1);&nbsp; return $l[$r];}$myVideoDir = ".";$dirList = scandir($myVideoDir);$videoList = array()foreach($dirList as $d){&nbsp; if(is_dir($d)){&nbsp; &nbsp; array_merge($videoList, getFileList($d, "mp4"));&nbsp; }}echo "<video id='dep' class='center' width='400' autoplay controls>\r\n";echo "\t<source src='$myVideoDir/" . pickRandVid($videoList) . "' type='video/mp4' />\r\n";echo "</video>\r\n";?>
随时随地看视频慕课网APP
我要回答