Click to See Complete Forum and Search --> : How easy is this?
Teach
09-05-2004, 06:40 PM
Hiya.
Hope you can help please!
We are setting up a LAN server for music, and have different anthems etc on there (yes...not illegal ones before you ask!), different remixes of our clan's songs and snippets, etc.
Basically, these are organised inside a directory on our unix server.
Let's say this directory is called songs, they're organised like:
/home/user/songs/A
..
/home/user/songs/Z
A through to Z.
Basically I need to create a PHP search script that can basically "grep" the searched text to the filenames of all the songs in about 3 depth directories and then show the results with a link, i.e. www.host.com/A/foo.mp3
Is this easily achieved?
Thanks.
MstrBob
09-05-2004, 08:04 PM
I'm a bit confused. You want to create a search function to search through your songs? Yes this is easily acheived. Maybe something like this: On one page you have a form in which a user enters text. Now you have it's action to a php page where you find a song that matches:
<?
/* checks to see if form field "query" was filled out. This is the text the usrer enters and we'll try to match */
if(isset($_POST['query']))
{
$dir = 'songs';
// opens directory
$dh = opendir($dir);
// reads through directory
while (false !== ($file = readdir($dh)))
{
if($file != '.' && $file != '..')
{
// stores filenames in array
$files[]=$file;
}
}
if(is_null($files))
{
$files[0] = 0;
}
// search array for matches
foreach($files as $filename)
{
//if a filename contains the user's
//text, display a link.
if(preg_match("/$_POST['query']/i", $filename))
{echo('<a href="http://www.yoursite.com/songdirectory/'.$filename.'">'.$filename.'</a>');}
}
} else {
echo('Please properly fill out the Search Form.');
}
?>
Note, that script is untested, and will only search the filename for a match. But I hope that gets you in the right direction.
To search through all directories only one level deep would be easy - as MstrBob has shown us - but you'll need a recursive function to search more than one level deep in each directory where another directory exists. If you want to do a fuzzy search (find similar results rather than exact results), the script will become much more complex. Also, I would suggest using the $_GET method for any searching script, rather than the $_POST method.
Hope this helps.
Teach
09-06-2004, 08:32 AM
Thank you so very much in advance.
That's a great start and I'm sure we can add our requirements to it.
You're a star, thank you. :)
Teach
09-06-2004, 10:54 AM
Hi again.
Okay, hope you can help me with a few more things...I've updated the script somewhat, and fixed an error from the original but I'm completely stumped on how to make it:
a) search up to 3 directories deep, for example in 'songs' there are folders called 'a' thorugh to 'z', so it needs to be able to them if possible.
b) when a match isn't found, the message "Sorry. No match found - please try again." is shown multiple times, it would seem the amount of files it's searching. Is there a way to get this to only occur once?
Thanks if you can help.
<?php
if (empty($_POST['query'])) {
echo "<p>Please complete the search box.</p>\n";
exit();
}
if(isset($_POST['query']))
{
$dir = 'songs';
$dh = opendir($dir);
while (false !== ($file = readdir($dh)))
{
if($file != '.' && $file != '..')
{
$files[]=$file;
}
}
if(is_null($files))
{
$files[0] = 0;
}
foreach($files as $filename)
{
if(preg_match("/{$_POST['query']}/i", $filename))
{
echo('<p><a href="http://www.url.com/songdirectory/'.$filename.'">'.$filename.'</a></p>');
} else {
echo('<p><strong>Sorry. No match found - please try again.</strong></p>');
}
}
} else {
echo('Please properly fill out the Search Form.');
}
?>
For your first problem, as I already mentioned, you'll need a recursive function. MstrBob may have gotten in too deep, but if he's willing to do it, I won't do it for him. ;)
As far as the error being printed out multiple times, you should be able to stop that with a counting variable.
<?php
$err = 0;
if (empty($_POST['query'])) {
echo "<p>Please complete the search box.</p>\n";
exit();
}
if(isset($_POST['query']))
{
$dir = 'songs';
$dh = opendir($dir);
while (false !== ($file = readdir($dh)))
{
if($file != '.' && $file != '..')
{
$files[]=$file;
}
}
if(is_null($files))
{
$files[0] = 0;
}
foreach($files as $filename)
{
if(preg_match("/{$_POST['query']}/i", $filename))
{
echo('<p><a href="http://www.url.com/songdirectory/'.$filename.'">'.$filename.'</a></p>');
} else {
echo('<p><strong>Sorry. No match found - please try again.</strong></p>');
}
}
} else {
if($err==0){
echo('Please properly fill out the Search Form.');
$err = 1;
}
}
?>
Teach
09-06-2004, 02:00 PM
Hi,
Thanks for the help Jona.
Unfortunately it still seems that the message "Sorry. No match found - please try again." appears multiple times with that code Jona. It appears 3 times, which is the exact amount of test .mp3 files I have in the 'songs' directory...?
Thanks.
sciguyryan
09-06-2004, 02:24 PM
Try this:
Try this:
<?php
$err = 0;
$Error = false;
if (empty($_POST['query'])){
echo "<p>Please complete the search box.</p>\n";
exit();
}
if (isset($_POST['query'])){
$dir = 'songs';
$dh = opendir($dir);
while (false !== ($file = readdir($dh))){
if(($file != ".") && ($file != "..")){
$files[]=$file;
}
}
if(is_null($files)){
$files[0] = 0;
}
foreach($files as $filename){
if(preg_match("/{$_POST['query']}/i", $filename)){
echo('<p><a href="http://www.url.com/songdirectory/'.$filename.'">'.$filename.'</a></p>');
}
else{
if ($Error != true){
echo('<p><strong>Sorry. No match found - please try again.</strong></p>');
$Error = true;
}
}
}
else {
if($err==0){
echo('Please properly fill out the Search Form.');
$err = 1;
}
}
?>
RyanJ
MstrBob
09-06-2004, 02:30 PM
Okay, well, I've included a file. I got it to work so that it searches 3 directories deep (all the directories) and returns but one error message. Worked for me, hope it works.
Teach
09-06-2004, 05:48 PM
Thank you so much!
There's just one error, though it doesn't seem to stop the script working which is good.
The error is on the line:
foreach($folders as $folder)
and I get the error:
Warning: Invalid argument supplied for foreach()
Also, is there a way to make the script only yield results for the whole word that's searched for? Or does it already currently do this?
Thanks again so so much.
MstrBob
09-06-2004, 08:29 PM
Okay, added a little error checking for the issue. But I don't understand what you mean by searching for only the word. The script checks the filenames to see if they contain the word the user enters and returns all results.