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