首页 > 解决方案 > 按大小排序scandir

问题描述

我什至不确定这是否可能,因为我已经查看了超过 15 篇关于按大小排序的 stackoverflow 帖子,但所有解决方案似乎都不适用于我的特定需求。

我的代码:

<div class="container">
<?php
$viddir = $_GET['vid'];
$perpage = 10;
$page = (int)$_GET['page'];
if(!($page>0)) $page = 1;
$offset = ($page-1)*$perpage;
$video_dir = "$viddir/Videos/";
$videos = scandir($video_dir);
arsort($videos);
$ignore = array(".", "..", "index.php");
$total_files = sizeof($videos);
$total_pages = ceil($total_files/$perpage);

$videos1 = array_slice($videos, $offset, $perpage);
    foreach($videos1 as $curvid){
        if(!in_array($curimg, $ignore)) {
            echo "<a href=\"$video_dir$curvid\"><video controls loop><source src=\"". $video_dir . '/' . $curvid ."\"></video></a>\n" ;
        }
    } ?>
    </div>

我想按视频的大小/长度对其进行排序,这甚至可以在不破坏我的代码的情况下进行吗?我已经尝试了一些 glob 解决方案,但它们都不能满足我的需要。

如果有人能看到其他解决方案,那将意味着很多,将不胜感激。

谢谢你的时间。

标签: phphtml

解决方案


做一个初始循环$videos并构建一个包含视频路径的新数组,$video_dir$curvid并使用 PHP 函数获取该文件的文件大小filesize( filename )

$videoList = [];
foreach( $videos1 as $curvid ) {
  $videoList[] = array( 'filepath' => $video_dir$curid, 'filesize' => filesize( $video_dir$curid ) );
}

usort($videoList, function ($item1, $item2) {
  return $item1['filesize'] <=> $item2['filesize'];
});

foreach( $videoList as $video ) {
  // Build your html using $video['filepath']
}

上面的代码来自我的脑海,未经测试可能需要一些更改,但应该有助于让您朝着正确的方向前进。

按视频长度排序有点复杂,您可能需要使用库。


推荐阅读