PHP Sort function by date (JWPLAYER auto playlist script)

家住魔仙堡 提交于 2019-12-13 07:39:19

问题


This will help anyone with JWPLAYER to make a playlist by scanning a folder and creating the correct XML file automatically.

I would like to add one function which I can not work out.

I would like to order the play list by name or date.

 $folder = opendir($path);
$start="<asx version='3.0'>n<title>Example ASX playlist</title>";
$Fnm = "./playlist.xml";
$inF = fopen($Fnm,"w");
fwrite($inF,$start."n");
while( $file = readdir($folder) ) {
     if (($file != '.')&&($file != '..')&&($file != 'index.htm')){
     $result="<entry>n<title>$file</title>n<ref href='$path2$file'/>n<param name='image' value='preview.jpg'/>n</entry>n";
         fwrite($inF,$result);
     }
}
fwrite($inF,"</asx>");
closedir($folder);
fclose($inF);
?>

Question:

I would like to add to the above code a sort function by date before creating the XML and list.

Thanks


回答1:


You have to loop the directory and get the date with filemtime and dump it in an array, here's a working script, you may change $path,$xmlfile or arsort() to sort() depanding on your needs ...

<?php

$xmlfile = "playlist.xml";
$path = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['SCRIPT_NAME']). "/musicfolder";
$folder = scandir($path);
$files = array();
foreach($folder as $file){
    if($file == '.' OR $file == '..' OR $file == 'index.htm'){}else{
        $files[$file] = filemtime($path.'/'.$file);
    }
}
arsort($files);

//use asort to sort from old to new

$output="<asx version='3.0'>" . PHP_EOL . "<title>Example ASX playlist</title>";
foreach($files as $file => $date){
$output .= "<entry>" . PHP_EOL . "<title>$file</title>" . PHP_EOL . "<ref href='$path'/>" . PHP_EOL . "<param name='image' value='preview.jpg'/>" . PHP_EOL . "</entry>" . PHP_EOL;
}
$output .= "</asx>";
file_put_contents($xmlfile,$output);
?>



回答2:


Make an array with the files' name, datetime (using filemtime($file)) etc and sort accordingly.



来源:https://stackoverflow.com/questions/10751794/php-sort-function-by-date-jwplayer-auto-playlist-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!