Log in

View Full Version : Search Directory



Jeffreyv1987
09-22-2009, 10:13 PM
I need help.... Ugh Alright ive got this code


<?php
if ($handle = opendir('./')) {
while (false !== ($file = readdir($handle))) {
$filesplit = explode(".", $file);
$check = $filesplit[0];
$keyword=$_GET['search'];

if(ereg($keyword, $check))
{
$check .= ".$filesplit[1]";
$valid[] = $check;

}
}

echo "Search Term: <i>$keyword</i> - <BR>";
for($index = 0; $index < count($valid); $index++)
{

echo"<font size=2> <A href='$valid[$index]'>$valid[$index]</a></font><BR>";
}
closedir($handle);

}
?>

What this does is it searches my directory for what ever keyword is put in but..

If i have an item in my directory called " Music " and i type in " music " with a lower case m nothing shows, so how would i make it non case sensitive?

That way i can look up Music or music and it'll show.

djr33
09-23-2009, 12:35 AM
The easiest way to deal with issues of capitals and lowercase is to force all strings to be one or the other.
strtolower() will take any string ("Music", "music") and output only lowercase ("music" for both). (strtoupper() exists too, if you'd rather use that.)
That way you can match everything.
Remember to apply the function to both things you are comparing (the search phrase and the filenames).

There are also some other functions that deal with this, like array sorting functions that ignore case, but that's probably beyond what you need here.

Hope this gets you started.

Jeffreyv1987
09-23-2009, 01:43 AM
Alright but when i upload files into a directory say i upload a file named ' Music001.mp3 ' useing my FTP program how would i make that work?

I've got the strtolower working but its not reading my file cause its an uppercase M in the directory.

traq
09-23-2009, 02:13 AM
I never use uppercase letters in my web directories or filenames (or just about anywhere else, if I can avoid it) for just that reason.

for example, this will work on some webhosts, but not others:
html:

<img src="picture.jpg">
filename on server:

picture.JPG