Hi,

I have written a script which reads files from a directory to build an xml file. Everything works great exepct on the mac, hidden files are included in the final output. is there any way to filter these out?

<?php

$imageDir = '../mp3/';
$extensions = array(".mp3", ".MP3", ".wav",".WAV");

if ($folder = opendir($imageDir)) {

   $filenames=array();

	while (false !== ($file = readdir($folder))) {

	$dot = strrchr($file, '.');
    if(in_array($dot, $extensions)){
		array_push($filenames, $file);

	} 		
   }

   $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
   $output .= "<songs>\n";
   foreach ($filenames as $source) {
      $newPath = str_replace("../","",$imageDir);
        $imageName = str_replace("_"," ",$source);
        $imageName = str_replace($extensions,"",$imageName);
        $output .= "\t<song src=\"$newPath$source\">$imageName</song>\n";
   }
   $output .= "</songs>";

   closedir($folder);

   $fp = fopen("../xml/musicPlayer/songs.xml", "w");
   fwrite($fp, $output);
   fclose($fp);

   }
?>

thanks,
Gareth

    Assuming that the names of hidden files begin with a period (as in dot), simply exclude all files whose names begin with a period.

      I just thought that you are doing the directory scanning the hard way so hope you dont mind I changed it a bit 😉 It will probably solve your problem with mac also.

      <?php
      $songDir = '../mp3/';
      $xmlDir = 'mp3/';
      $extensions = array('mp3','wav');
      
      $output = array();
      $output[] = '<?xml version="1.0" encoding="utf-8"?>';
      $output[] = '<songs>';
      
      foreach (glob($songDir.'*.*') as $f)
      {
              $pinfo = pathinfo($f);
      
          if (in_array(strtolower($pinfo['extension']),$extensions))
          {
                  $songName = str_replace('_',' ',$pinfo['filename']);
                  $output[] = "\t<song src=\"{$xmlDir}{$pinfo['basename']}\">{$songName}</song>";
          }
      }
      
      $output[] = '</songs>';
      
      file_put_contents('../xml/musicPlayer/songs.xml',implode("\n",$output));
      

      That file_put_contents is php5 only but you can use the fopen-fwrite-flose method if you want.

        Write a Reply...