...

View Full Version : Script to search for local files.



torrjuck
11-25-2010, 02:05 AM
Hi,

I need help to make a script that checks files in a local folder and then letting a user search for a part of the filename. If the file that the user searches for exists it should output a download link.
This is my code:

<?PHP
$dir = "files/";
$handle = opendir($dir);
while ($folder = readdir($handle))
{
$folders[] = $folder;
}
closedir($handle);
foreach ($folders as $folder) {
echo "<a href=$dir$folder>$folder</a>"."<br />";
}
?>

I have a search for like this:
<form action="" method="post" id="formen">
<input id="find" type="text" name="find" size="30" />
<button name="submit" type="submit" value="Skicka" tabindex="6">find</button>
</form>

How can i let the users get a download link only if the thing they are searching for exists?

Please help me.

Fou-Lu
11-25-2010, 02:23 AM
Hi, in the future could you please wrap your code in
tags to preserve the format (I know its kinda short here, but longer ones are impossible to read without).
The easiest way to do what you are looking to do, which is a scalar search for a file in a single directory is to use the glob() function. It takes a simple pattern and finds results:


$toFind = htmlspecialchars($_POST['find']);
$dir = "files/";
foreach (glob($dir . $toFind) AS $found)
{
echo $found; // Throw your links in here.
}


Given the simplistic parsing of glob, you can actually get relatively complex with them without using preg.

torrjuck
11-25-2010, 02:37 AM
Thank you very much!
But now im am forced to enter the exact filename in the search-form. Is it possible to only enter a part of it?

Fou-Lu
11-25-2010, 02:43 AM
Yes, you need to wildcard it. This method really only works if you leave the extension off, but if you want to allow it you can use parse_url to remove the extension bit, or even use it to your advantage.


$toFind = "*" . htmlspecialchars($_POST['find']) . "*";

That wildcard should let you search for 'find' anywhere in the middle. Like I said, no extension though on this one (see the parse_url on php.net to remove the extension if you like. Basename is another option).

torrjuck
11-25-2010, 09:17 PM
Thank you, but something is still wrong. Its not returns all search-results:mad:.
Can you please take one more look?



$dir = "files/";
$handle = opendir($dir);
while ($folder = readdir($handle))
{
$folders[] = strtolower($folder);
}
closedir($handle);
foreach ($folders as $folder) {
//echo "<a href=$dir$folder>$folder</a>"."<br />";
}


if(isset($_POST["find"])) {
$sokdata = strtolower($_POST["find"]);
$toFind = "*" . htmlspecialchars($sokdata) . "*";
$dir = "files/";
$found = strtolower($found);
foreach (glob($dir . $toFind) as $found)
{
echo "<a href=$found>$found</a>"."<br />";
}
}

Fou-Lu
11-26-2010, 12:14 AM
Thank you, but something is still wrong. Its not returns all search-results:mad:.
Can you please take one more look?



$dir = "files/";
$handle = opendir($dir);
while ($folder = readdir($handle))
{
$folders[] = strtolower($folder);
}
closedir($handle);
foreach ($folders as $folder) {
//echo "<a href=$dir$folder>$folder</a>"."<br />";
}


if(isset($_POST["find"])) {
$sokdata = strtolower($_POST["find"]);
$toFind = "*" . htmlspecialchars($sokdata) . "*";
$dir = "files/";
$found = strtolower($found);
foreach (glob($dir . $toFind) as $found)
{
echo "<a href=$found>$found</a>"."<br />";
}
}



I just upgraded to Windows 7 and haven't installed PHP or anything yet. Does it find any results at all, or just no results? If you print the $sokdata, what is in it?

torrjuck
11-26-2010, 06:38 AM
Its only echo the letters that i entered in the "find"-form, but in lowercase.
I think it's something wrong with the lowercase-commandos somewere. If the files only contains lowercase letters it will find the file, but if it contains uppercase letters its harder to enter something in the form to make it find the file. If the filename is ABcDE i need to enter "c" or "C" in the find-form. It seems that the variable $found doesnt transforms to lowercases. What is wrong?

Fou-Lu
11-26-2010, 06:54 PM
Nothing wrong with that I'm afraid. *nix systems are case sensitive, so that is correct.
If you check the glob api: http://php.ca/manual/en/function.glob.php, the first user comment there is actually a block of code to write a function called globistr, which is a case-insensitive look.

torrjuck
12-03-2010, 08:46 AM
Nothing wrong with that I'm afraid. *nix systems are case sensitive, so that is correct.
If you check the glob api: http://php.ca/manual/en/function.glob.php, the first user comment there is actually a block of code to write a function called globistr, which is a case-insensitive look.

I do not fully understand. Can you please write a little bit code so i understand?
Thank you very muck for all the help!

Lamped
12-03-2010, 08:52 AM
You could always go the less efficient route:


$folder = scandir('folder');
foreach($folder as $file) {
$file = basename($file); // basic sanitisation
if (is_file($folder.'/'.$file) and (stripos($file, $_POST["find"]) !== false)) {
echo('<a href="'.$folder.'/'.$file.'">Download</a>');
}
untested.

stripos does a case insensitive search in the filename



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum