Jump to content

recursive dir scan - someone help !


map200uk

Recommended Posts

hi, as asked before, im having troubles with my recursive directory scan

 

<?php
  	 function recur_dir($dir)
  	 {
 $dirlist = opendir($dir);
     
	 while ($file = readdir ($dirlist))
     {
	 if ($file != '.' && $file != '..')
  	         {
             $newpath = $dir.'/'.$file;
	             $level = explode('/',$newpath);
             if (is_dir($newpath))
  	             {
				$parts=pathinfo($newpath);
				$extension=$parts['extension'];
              recur_dir($newpath);
  	             }else{
				$parts=pathinfo($newpath);
				$extension=$parts['extension'];
		if($extension=="mp3")
		{
		echo "mp3 found, file : => $newpath";
		$mp3[]=$newpath;

		}
	echo "<br>File \n $newpath  has extension  equal to  $extension<br>";
         }
	         }
	}
	echo "<br><br><br><br>";
	print_r($mp3);
  	     closedir($dirlist); 
  	     return $mod_array;

 }
	 echo '<pre>';
  	 print_r(recur_dir('.'));
	 echo '</pre>';



 recur_dir("/home/map/www.vcd.cl");
	 ?>

 

when an mp3 is found i need it to add the entry to an array, and then this array would get returned containing all the found mp3s, altho i cannot seem to get it to work?!

 

help greatly appreciated

 

mark

Link to comment
https://forums.phpfreaks.com/topic/48140-recursive-dir-scan-someone-help/
Share on other sites

<?php
function findMusic($dir) {
     $musicList = array();
     $dirHandle = opendir($dir);
     while (($file = readdir($dirHandle)) !== FALSE) {
          if ($file == "." || $file == "..") continue;
          if (is_dir($dir . "/" . $file)) $musicList = array_merge($musicList, findMusic($dir . "/" . $file));
          else {
               $extension = end(explode(chr(46), $file));
		   print $extension;
               if (strtolower($extension) == "mp3") { $musicList[] = $dir . "/" . $file; } 
          }
     }
     closedir($dirHandle);
     return $musicList;
}
print_r(findMusic("C:/Documents and Settings/Your Username/My Documents/My Music"));
?>

Made a slight mistake in the first code.  So, if you copied it before my last edit time in this post, try copying it again before you post that it doesn't work :).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.