PHP find mp3's in folder and sub folders

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

PHP find mp3's in folder and sub folders

Post 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>"; 
}
}
}
}
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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);
}

?>
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's a function.. call it.

Code: Select all

list($dirs, $files) = directoryTraverse('media', true);
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

Post 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); 

?>
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You don't add it to the function internals. You use the returned arrays to do the renaming.
comedydave
Forum Newbie
Posts: 16
Joined: Thu Mar 09, 2006 8:51 am

Post by comedydave »

So after

Code: Select all

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

Thanks,

Dave
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

after

Code: Select all

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