Php 如何仅显示.mp4文件?
专家们好 我目前正在增强我的treeview项目,但我只会显示某些文件类型。我只想在我的树视图中显示mp4类型的文件。代码如下: (计入Treeview的原始代码)Php 如何仅显示.mp4文件?,php,Php,专家们好 我目前正在增强我的treeview项目,但我只会显示某些文件类型。我只想在我的树视图中显示mp4类型的文件。代码如下: (计入Treeview的原始代码) 类树视图 { 私有$root; 公共函数构造($path) { $this->root=$path; } 公共函数getTree() { 返回$this->createStructure($this->root,true); } 私有函数createStructure($directory,$root) { $structure=$
类树视图
{
私有$root;
公共函数构造($path)
{
$this->root=$path;
}
公共函数getTree()
{
返回$this->createStructure($this->root,true);
}
私有函数createStructure($directory,$root)
{
$structure=$root?“”:“”;
$nodes=$this->getNodes($directory);
foreach($node作为$node){
$path=$directory.'/'.$node;
if(is_dir($path)){
$structure.='- ;
$structure.=''.$node';
$structure.=self::createStructure($path,false);
$structure.='
';
}否则{
$path=str_replace($this->root.'/',null,$path);
$structure.='- ;
$structure.='';
$structure.='
';
}
}
返回$structure.“
”;
}
私有函数getNodes($directory=null)
{
$folders=[];
$files=[];
$nodes=scandir($directory);
foreach($node作为$node){
如果(!$this->exclude($node)){
if(is_dir($directory.'/'.$node)){
$folders[]=$node;
}否则{
$files[]=$node;
}
}
}
返回数组合并($folders,$files);
}
私有函数排除($filename)
{
以数组形式返回($filename,['.','..,'index.php','.htaccess','.DS_Store']);
}
}
$treeView=新的treeView('movies/');
echo$treeView->getTree();
您可以提供允许的文件扩展名的“白名单”作为上述类的第二个参数,并将发现的任何文件的扩展名与该白名单进行比较,从而控制返回的文件
class TreeView {
private $root;
private $whitelist;
public function __construct($path, $whitelist=array() ){
$this->root = $path;
$this->whitelist=$whitelist;
}
public function getTree(){
return $this->createStructure($this->root, true);
}
private function createStructure($directory, $root){
$structure = $root ? '<ul class="treeview">' : '<ul>';
$nodes = $this->getNodes($directory);
foreach ($nodes as $node) {
$path = $directory.'/'.$node;
if (is_dir($path) ) {
$structure .= '<li class="treeview-folder"><p>';
$structure .= '<details class="details-example"><summary><font size="5px">'.$node.'</font></summary>';
$structure .= self::createStructure($path, false);
$structure .= '</p></li>';
} else {
$path = str_replace($this->root.'/', null, $path);
$structure .= '<li class="treeview-file"><p>';
$structure .= '<a href="movies/./'.$path.'"><font size="5px">'.$node.'</font></a>';
$structure .= '</p></li>';
}
}
return $structure.'</ul>';
}
private function getNodes($directory = null){
$folders = [];
$files = [];
$nodes = scandir($directory);
foreach ($nodes as $node) {
if (!$this->exclude($node)) {
if (is_dir($directory.'/'.$node)) {
$folders[] = $node;
} else {
# check the whitelist for the file extension
if( !empty( $this->whitelist ) ){
$ext=strtolower( pathinfo( $node, PATHINFO_EXTENSION ) );
if( in_array( $ext, $this->whitelist ) ) $files[] = $node;
}else{
$files[] = $node;
}
}
}
}
return array_merge($folders, $files);
}
private function exclude($filename){
return in_array($filename, ['.', '..', 'index.php', '.htaccess', '.DS_Store']);
}
}
$whitelist=array('mp4');
$treeView = new TreeView('movies/',$whitelist);
echo $treeView->getTree();
类树视图{
私有$root;
私人$白名单;
公共函数构造($path,$whitelist=array()){
$this->root=$path;
$this->whitelist=$whitelist;
}
公共函数getTree(){
返回$this->createStructure($this->root,true);
}
私有函数createStructure($directory,$root){
$structure=$root?“”:“”;
$nodes=$this->getNodes($directory);
foreach($node作为$node){
$path=$directory.'/'.$node;
if(is_dir($path)){
$structure.='- ;
$structure.=''.$node';
$structure.=self::createStructure($path,false);
$structure.='
';
}否则{
$path=str_replace($this->root.'/',null,$path);
$structure.='- ;
$structure.='';
$structure.='
';
}
}
返回$structure.“
”;
}
私有函数getNodes($directory=null){
$folders=[];
$files=[];
$nodes=scandir($directory);
foreach($node作为$node){
如果(!$this->exclude($node)){
if(is_dir($directory.'/'.$node)){
$folders[]=$node;
}否则{
#检查文件扩展名的白名单
如果(!empty($this->whitelist)){
$ext=strtolower(路径信息($node,路径信息_扩展));
如果(在数组中($ext,$this->whitelist))$files[]=$node;
}否则{
$files[]=$node;
}
}
}
}
返回数组合并($folders,$files);
}
私有函数排除($filename){
以数组形式返回($filename,['.','..,'index.php','.htaccess','.DS_Store']);
}
}
$whitelist=数组('mp4');
$treeView=newtreeview('movies/',$whitelist);
echo$treeView->getTree();
上述PHP将生成无效的HTML
应该是另一种方式-结束标记对也是如此。aP
不是ul
的有效孩子,但它可以是li的孩子这正是我要找的,Abronsius教授先生!!!我真的很感谢你的帮助。现在,我的学生在我们学校的本地网站上播放我们的视频时不会感到困惑!!^ ^。非常感谢你,先生。上帝保佑你,先生。
class TreeView {
private $root;
private $whitelist;
public function __construct($path, $whitelist=array() ){
$this->root = $path;
$this->whitelist=$whitelist;
}
public function getTree(){
return $this->createStructure($this->root, true);
}
private function createStructure($directory, $root){
$structure = $root ? '<ul class="treeview">' : '<ul>';
$nodes = $this->getNodes($directory);
foreach ($nodes as $node) {
$path = $directory.'/'.$node;
if (is_dir($path) ) {
$structure .= '<li class="treeview-folder"><p>';
$structure .= '<details class="details-example"><summary><font size="5px">'.$node.'</font></summary>';
$structure .= self::createStructure($path, false);
$structure .= '</p></li>';
} else {
$path = str_replace($this->root.'/', null, $path);
$structure .= '<li class="treeview-file"><p>';
$structure .= '<a href="movies/./'.$path.'"><font size="5px">'.$node.'</font></a>';
$structure .= '</p></li>';
}
}
return $structure.'</ul>';
}
private function getNodes($directory = null){
$folders = [];
$files = [];
$nodes = scandir($directory);
foreach ($nodes as $node) {
if (!$this->exclude($node)) {
if (is_dir($directory.'/'.$node)) {
$folders[] = $node;
} else {
# check the whitelist for the file extension
if( !empty( $this->whitelist ) ){
$ext=strtolower( pathinfo( $node, PATHINFO_EXTENSION ) );
if( in_array( $ext, $this->whitelist ) ) $files[] = $node;
}else{
$files[] = $node;
}
}
}
}
return array_merge($folders, $files);
}
private function exclude($filename){
return in_array($filename, ['.', '..', 'index.php', '.htaccess', '.DS_Store']);
}
}
$whitelist=array('mp4');
$treeView = new TreeView('movies/',$whitelist);
echo $treeView->getTree();