简体   繁体   中英

Sorting scandir by size

I am not even sure if it's possible because i've looked at probs over 15 stackoverflow posts about sorting by size, but all of the solutions don't seem to work for my specific need.

My code:

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

And i wanna sort it by size/length of the video, is this even possible without breaking my code? I've tried a few glob solutions but none of them work for what i need.

If anyone can see some other solution to this, it would mean alot and would be greatly appreciated.

Thanks for your time.

Do an initial loop of your $videos and build a new array that contains the video path $video_dir$curvid and get the file size of that file using the PHP function 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']
}

Above code is from my head and isn't tested some changes may be needed, but should help to get you in the right direction.

Sorting by video length is a bit more involved you will probably require use of a library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM