亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從文件夾中隨機播放視頻文件,遞歸地向下查看所有目錄結構樹

從文件夾中隨機播放視頻文件,遞歸地向下查看所有目錄結構樹

PHP
青春有我 2023-05-26 10:02:08
一些 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>    ';}?>
查看完整描述

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";


?>


查看完整回答
反對 回復 2023-05-26
  • 1 回答
  • 0 關注
  • 201 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號