Jump to content

Recommended Posts

Hi Guys,

I need the code to be able to search a folder and all folders within it, and then all folders within those sub-folders. The following code will search a folder and all subfolders in it, but i'm not sure how to get it to carry on searching within those subfolders and so on...

[code]<?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>";
}
}
}
}
?>[/code]

It basically checks the first folder for mp3 files that have the search text in it, if it finds a mp3 it displays it, if it finds a folder, it will then search that for mp3s and display them, but then i can't get it to search the next folders if it finds one!

Any ideas?

I'm sure it's quite easy but can't figure it out.

Thanks alot,

Dave
Link to comment
https://forums.phpfreaks.com/topic/4516-search-subfolders/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.