1 回答
TA贡献1816条经验 获得超4个赞
我建议使用以下代码。
<?php
$myVideoDir = '.';
$extension = 'mp4';
$videoFile = false;
$pseudoDir = scandir($myVideoDir);
$myitems = array();
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 <> "" ) {
array_push($myitems, $videoFile);
}
}
}
}
$myrandom = rand(0,count($myitems)-1);
if ( !!$videoFile ) {
echo '<video id="dep" class="center" width="400" autoplay controls><source src="'.$myVideoDir.'/'.$myitems[$myrandom].'" type="video/mp4"></video>';
}
?>
更新
考虑做一个函数。
<?php
function getFileList($dirPath, $ext){
$list = scandir($dirPath);
$fileList = array();
foreach($list as $item) {
if ($item != '..' && $item != '.' && !is_dir($item)) {
$info = pathinfo($item);
$videoFile = $item;
if ($info['extension'] == $ext) {
array_push($fileList, $item);
}
}
}
return $fileList;
}
function pickRandVid($l){
$r = rand(0, count($l) - 1);
return $l[$r];
}
$myVideoDir = ".";
$dirList = scandir($myVideoDir);
$videoList = array()
foreach($dirList as $d){
if(is_dir($d)){
array_merge($videoList, getFileList($d, "mp4"));
}
}
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";
?>
- 1 回答
- 0 关注
- 157 浏览
添加回答
举报