Directory search script

This is where you share YOUR scripts with others

Moderators: macek, egami, gesf

GoldenBear
New php-forum User
New php-forum User
Posts: 1
Joined: Mon Feb 06, 2012 11:07 am

Directory search script

Postby GoldenBear » Mon Feb 06, 2012 11:27 am

I am sharing the form/script below and appealing for help in expanding its capabilities.

I have a search form and php script which return a page in a browser which contains the sought after file in the form of a hyperlink which allows the file to be downloaded.

What I am trying to accomplish is the ability to type, or copy and paste, a list of items to be searched for simultaneously into the search box and have all of the found items returned in the browser in the form of a link to each of the files.

I have been trying to use a textarea form, as it will allow multiline input, but I can't figure out how to make the input name and input type control the textarea so that its contents will be searched for.


This is the form currently embedded in the html page:

form action="my_search_script.php" method="get"><big><input name="q"
type="text"> <input type="submit"></big></form>


This is the current script (my_search_script.php) which searches the directory where the files reside:

<?php
$dir = 'my_image_directory';
$exclude = array('.','..','.htaccess');
$q = (isset($_GET['q']))? strtolower($_GET['q']) : '';
$res = opendir($dir);
while(false!== ($file = readdir($res))) {
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) {
echo "<a href='$dir/$file'>$file</a>";
echo "<br>";
}
}
closedir($res);
?>

Any help is greatly appreciated.

Thank you,

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Directory search script

Postby egami » Mon Feb 06, 2012 1:35 pm

you can do it with a textarea just fine.
you just have to separate it using the \n

for example.

$var = $_POST['textarea'];
$var = explode("\n",$var); // Now $var is an array of lines.
foreach($var as $k => $v) { // $v is the line you're looking for..
do your stuff here..
}


and done.
Pretty easy really.


Return to “Your Scripts”

Who is online

Users browsing this forum: No registered users and 0 guests