Page 1 of 1

PHP find mp3's in folder and sub folders

Posted: Thu Mar 09, 2006 8:55 am
by comedydave
Hi guys,

I have a folder called "media" of which i want to search (including all subfolders, and subfolders within them) for mp3 files and then list them as hyperlinks to "playmedia.php?fileid=(file URL)"

Thanks alot,

Dave

If it helps, here's my code so far. It searches the folder "media" and subfolders and returns only mp3's, but i can't get it to search media -> subfolder -> subfolder

Code: Select all

<?php
$searchstring = $_REQUEST['searchstring'];
if ($searchstring == ""){
print "Please enter some text to search for!";
return;
}
$dir=opendir("media");
$files=array();
while (($file=readdir($dir)) !== false)
{
if ($file != "." and $file != ".." and $file != "index.php" and $file != "fileupload-class.php" and $file != "icon.gif" and $file != "launch.gif" and $file != "download.php" and $file != "index.asp" and $file != "images" and $file != "login_check.asp" and $file != "main.php" and $file != "delete.gif" and $file != "images" and $file != "delete.php" and $file != "spacer.gif" and $file != "ianstanton" and $file != "play_all.m3u" and $file != "play_all_shuffle.m3u")
 {
array_push($files, $file);
 }
}

closedir($dir);
sort($files);

foreach ($files as $file){
$ext = explode('.', $file);
$extension = $ext[count($ext)-1];
if (($extension == "mp3")&&(eregi("$searchstring", $file))) {
print "<table width='220' border='0'>
<tr><td align='left' valign'top'><a href='http://audiostation/playmedia.php?fileid=http://audiostation/media/$file&download=yes' target='player'><img src='images/music_icon.gif' alt='$file' border='0'></a><td></td><td width='205' style='font-family: Arial, Helvetica, sans-serif; font-size:10px;'><strong><a style='color:001F63;' class='body' href='playmedia.php?fileid=http://audiostation/playmedia.php?fileid=http://audiostation/media/$file&download=yes' target='player'>$file</a></strong></td></tr></table>"; 
}
if ($extension  !=  "mp3") {

$dir=opendir("media/$file");
$album=$file;
$files=array();
while (($file=readdir($dir)) !== false)
{
if ($file != "." and $file != ".." and $file != "index.php" and $file != "fileupload-class.php" and $file != "icon.gif" and $file != "launch.gif" and $file != "download.php" and $file != "index.asp" and $file != "images" and $file != "login_check.asp" and $file != "main.php" and $file != "delete.gif" and $file != "images" and $file != "delete.php" and $file != "spacer.gif" and $file != "ianstanton" and $file != "play_all.m3u" and $file != "play_all_shuffle.m3u")
 {
array_push($files, $file);
 }
}

closedir($dir);
sort($files);

foreach ($files as $file){
$get = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF']; 
$cur_dir = dirname($get);
$DirNow = dirname($get);
$DirUp = dirname($DirNow);
$ext = explode('.', $file);
$extension = $ext[count($ext)-1];
if (($extension == "mp3")&&(eregi("$searchstring", $file))) {
print "<table width='220' border='0'>
<tr><td align='left' valign'top'><a href='http://audiostation/playmedia.php?fileid=media/$album/$file&download=yes' target='player'><img src='images/music_icon.gif' alt='$file' border='0'></a><td></td><td width='205' style='font-family: Arial, Helvetica, sans-serif; font-size:10px;'><strong><a style='color:001F63;' class='body' href='playmedia.php?fileid=media/$album/$file&download=yes' target='player'>$file</a></strong></td></tr></table>"; 
}
}
}
}
?>

Posted: Thu Mar 09, 2006 8:58 am
by Chris Corbyn
You need a recursive function that indefinitely go into any folder inside any other folder until there are no more folders on each branch.

comedydave? Isn't that the guy from radio1? :P

Posted: Thu Mar 09, 2006 9:00 am
by comedydave
Yeh thats him!

What on earth is a recursive function?

Take it it's some kind of loop - i'm not too hot with coding!

Any ideas on how to do it?

Thanks alot,

Dave

Posted: Thu Mar 09, 2006 9:09 am
by feyd
recursive = bad. :P

Code: Select all

<?php

function directoryTraverse($directory, $searchSubdirectories = false)
{
	$directories = array($directory);
	$files = array();

	for($dir = 0; $dir < count($directories); $dir++) {
		$data = glob($directories[$dir] . DIRECTORY_SEPARATOR . '*');

		foreach($data as $file) {
			if(is_file($file)) {
				$files[] = $file;
			} elseif(is_dir($file)) {
				$directories[] = $file;
			}
		}

		if(!$searchSubdirectories) {
			break;
		}
	}
	
	return array($directories,$files);
}

?>

Posted: Thu Mar 09, 2006 9:16 am
by comedydave
Thanks for that feyd!

Not sure how the hell to use it though!

I presume i need to specify $directory in the first place?

Thanks alot!

Dave

Posted: Thu Mar 09, 2006 9:29 am
by feyd
it's a function.. call it.

Code: Select all

list($dirs, $files) = directoryTraverse('media', true);

Posted: Thu Mar 09, 2006 10:01 am
by comedydave
OMFG it worked!

Been trying to do this for 2 days - Thanks alot feyd!

For reference heres the final code i used!

Code: Select all

<?php 
function directoryTraverse($directory, $searchSubdirectories = false) 
{ 
$searchstring = $_REQUEST['searchstring'];
if ($searchstring == ""){
print "Please enter some text to search for!";
return;
}
    $directories = array($directory); 
    $files = array(); 

    for($dir = 0; $dir < count($directories); $dir++) { 
        $data = glob($directories[$dir] . DIRECTORY_SEPARATOR . '*'); 

        foreach($data as $file) { 
            if(is_file($file)) {
			$ext = explode('.', $file);
			$extension = $ext[count($ext)-1];
			if (($extension == "mp3")&&(eregi("$searchstring", $file))) { 
                $files[] = $file; 
				print"<img src='images/music_icon.gif' alt='$file' border='0'><img src='images/spacer.gif' border='0' width='5' ><a href='playmedia.php?fileid=$file&download=yes' target='player' style='font-family: Arial, Helvetica, sans-serif; font-size:10px; color:001F63'>$file</a><br><br>"; 
				}
            } elseif(is_dir($file)) { 
                $directories[] = $file; 
             
			
        } 
 } 
        if(!$searchSubdirectories) { 
            break; 
        } 
   
    } 
    return array($directories,$files); 
} 

list($dirs, $files) = directoryTraverse('media', true); 

?>

Posted: Thu Mar 09, 2006 10:07 am
by comedydave
Just a quick one,

Any way i can get it to say 'no files found' if it finds nothing?

Also, prob not possible, but can you display a gif (like a please wait) while it searches, then displays them when finished?

Thanks again!

Dave

Posted: Thu Mar 09, 2006 10:13 am
by feyd
comedyDave wrote:Any way i can get it to say 'no files found' if it finds nothing?
use a conditional against count($files) after calling the function.

[quote="comedyDave']Also, prob not possible, but can you display a gif (like a please wait) while it searches, then displays them when finished?[/quote]There are a few ways. The easier is using a bit of Javascript and CSS. Output a div that has the animated gif in it. Output another div for the data to populate in. You can either emit a bit of Javascript after that to switch the display settings around or use an onload handling snippet to do the same.

Posted: Fri Mar 10, 2006 8:04 am
by comedydave
Hi Feyd,

I have a bit of code that i wish to add to the search php page so as it goes through the folders and files, if it finds an illegal character it will rename it:

Code: Select all

$pattern = "/(!|-|')/";
 preg_match($pattern,$file,$matches); 
            if(!isset($matches[1])) 
continue;

            else 
            { 
                $fold = $file; 
                $file = preg_replace($pattern,"",$file);  
                rename("$fold","$file");
Just wondering if you had any ideas on how to implement it?

Thanks alot,

Dave

Posted: Fri Mar 10, 2006 8:55 am
by feyd
you could use my previous directoryTraversal function with basename() and dirname() to pull out just the file name or last directory name from both the files and directories arrays it returns. Rename the files first.

Posted: Fri Mar 10, 2006 10:09 am
by comedydave
Thanks for that,

I've tried putting it in after

Code: Select all

if(is_file($file)) {
and after

Code: Select all

$directories[] = $file;


but can't get it to work :-s

Thanks,

Dave

Posted: Fri Mar 10, 2006 10:22 am
by feyd
You don't add it to the function internals. You use the returned arrays to do the renaming.

Posted: Fri Mar 10, 2006 10:27 am
by comedydave
So after

Code: Select all

return array($directories,$files);
??

Thanks,

Dave

Posted: Fri Mar 10, 2006 10:32 am
by feyd
after

Code: Select all

list($dirs, $files) = directoryTraverse('media', true);