Dec 5, 2015
62 Views

PHP Auto create XML playlist file

Written by

List file create order by abc

<?php
// *** requires PHP5 ***
// search for jpg files

$filter = ".mp3";
// path to the directory you want to scan
$directory = './audio';
$it = new RecursiveDirectoryIterator("$directory");
foreach(
new RecursiveIteratorIterator($it) as $file)
{
  if (!((strpos(strtolower($file), $filter)) === false))
  {
    $items[] = preg_replace("#\\\#", "/", $file);
  }
}
sort($items);
header("content-type:text/xml;charset=utf-8");
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
echo "<playlist version='1' xmlns='http://xspf.org/ns/0/'>\n";
echo "  <title>CPN Playlist</title>\n";
echo "  <info>http://cpn.vn/</info>\n";
echo "  <trackList>\n";
foreach($items as $item)
{
  $title_array = explode('/', $item);
  $title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
  echo "    <track>\n";
  echo "      <title>"    . $title . "</title>\n";
  echo "      <location>" . $item  . "</location>\n";
  echo "    </track>\n";
}
echo "  </trackList>\n";
echo "</playlist>\n";


?>

List file create order by modifed

<?php
$directory = "./list/Vn";
chdir($directory);
array_multisort(array_map('filemtime', ($files = glob("*.{mp3}", GLOB_BRACE))), SORT_DESC, $files);
preg_replace("#\\\#", "/", $files);
header("content-type:text/xml;charset=utf-8");
echo "<?xml version='1.0' encoding='UTF-8'?>\n";
echo "<playlist version='1' xmlns='http://xspf.org/ns/0/'>\n";
echo "  <title>Oliver Playlist</title>\n";
echo "  <info>http://linholiver.com/</info>\n";
echo "  <trackList>\n";
foreach($files as $filename)
{
  $title_array = explode('/', $filename);
  $title = substr(end($title_array), 0, (strlen(end($title_array)) - 4));
  echo "    <track>\n";
  echo "      <title>"    . $title . "</title>\n";
  echo "      <location>" . $directory."/".$filename  . "</location>\n";
  echo "    </track>\n";
}  
echo "  </trackList>\n";
echo "</playlist>\n";
?>

 

Article Tags:
· · · · · · · · ·
Article Categories:
Code/Web
    http://linholiver.com

    https://linholiver.com/diary/about/