Forum Moderators: coopster

Message Too Old, No Replies

Searching Directory Files

Need simple Script

         

unclekracker23

11:33 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



Hello,

I have searched until I am blue in the face and cannot find what I need. I know I need to learn php and I have started a little but I am in a crunch.

I need a simple script that will display the contents of a directory on a web server with a link to each file for viewing. I also need a simple search function included that will search through the files listed in the directory.

Does anyone know where I can find something like this? Know how to write one? Please help, I am desperate.

Thanks,
uncle

unclekracker23

11:59 pm on Jun 19, 2007 (gmt 0)

10+ Year Member



So far I have this..

<?php
$dir = "c:/indigoperl/apache/htdocs/secure1";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh))!== false) {
echo "<a href='$file'>$file</a>";
echo "<br>";
}
closedir($dh);
}
}
?>

How do I add search and hide the htaccess file?

jatar_k

12:36 pm on Jun 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could add an exclusion

<?php
$dir = "c:/indigoperl/apache/htdocs/secure1";
$excluded = array('.htaccess');

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh))!== false &&!in_array($file,$excluded)) {
echo "<a href='$file'>$file</a>";
echo "<br>";
}
closedir($dh);
}
}
?>

unclekracker23

2:50 pm on Jun 20, 2007 (gmt 0)

10+ Year Member



jatar,

I copied and pasted your code and saved it as index.php and copied it to the directory. It's not showing any files in the directory now. I guess since the htaccess file is first in the list it stops there.

Thanks,
uncle

jatar_k

3:05 pm on Jun 20, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



could be this way

<?php
$dir = "c:/indigoperl/apache/htdocs/secure1";
$excluded = array('.htaccess');

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh))!== false) {
if (!in_array($file,$excluded)) {
echo "<a href='$file'>$file</a>";
echo "<br>";
}
}
closedir($dh);
}
}
?>

unclekracker23

6:19 pm on Jun 20, 2007 (gmt 0)

10+ Year Member



jatar,

Thanks, that done the trick as far as hiding the htaccess file.

Any idea how to implement a simple search feature into the script? The directory will probably have a couple thousand files in it and I would like to be able to offer a simple search for them to find the file they are looking for.

I know they can do this through there web browser but for the less pc inclined I would love to have a simple search box.

uncle

eelixduppy

6:43 pm on Jun 20, 2007 (gmt 0)



Try something like this:

$dir = 'path/to/directory';
$exclude = array('.','..','.htaccess');
$q = (isset($_GET['q']))? $_GET['q'] : '';
$res = [url=http://www.php.net/opendir]opendir[/url]($dir);
while(false!== ($file = [url=http://www.php.net/readdir]readdir[/url]($res))) {
if([url=http://www.php.net/stripos]stripos[/url]($file,$q)!== false &&![url=http://www.php.net/in-array]in_array[/url]($file,$exclude)) {
echo $file.'<br/>';
}
}
[url=http://www.php.net/closedir]closedir[/url]($res);

To set the search query, you put it in the uri like this:


page.php?q=search_terms

Good luck and Welcome to WebamsterWorld!

unclekracker23

11:37 pm on Jun 20, 2007 (gmt 0)

10+ Year Member



eelix,

I get an undefined function stripos error on your example. I also still dont understand how to implement your search suggestion into the script. Sorry for being a newb.

Thanks for the help and the welcome to the boards.

uncle

eelixduppy

12:57 am on Jun 21, 2007 (gmt 0)



You are getting an undefined function error because you don't have PHP 5. That's ok, you just have to work around the case-sensitive problem. Try the following:

$dir = 'path/to/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 $file.'<br/>';
}
}
closedir($res);

And to make the directory searchable through a form, you have to set the method to GET, like the following example:


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

Where page.php contains the php code I have above.

unclekracker23

1:55 am on Jun 21, 2007 (gmt 0)

10+ Year Member



eelix,

THANKS! This is sooo what I am looking for. It works great. Is there a way to make the found file a clickable link to the file location (for viewing).

Also, is there a way to add a back button in the page.php page to go back to index.html(search page)?

I so appreciate all of your help and I do apologize if I am asking for too much from you.

Thanks again,
uncle

unclekracker23

2:17 am on Jun 21, 2007 (gmt 0)

10+ Year Member



eelix, I just wanted to say thanks again for everything. I figured out the answers to my own questions by using the echo command and some html. It works perfectly for what I need.

Thanks again!
uncle

PS. I am so glad I found this site. Thanks everyone for being so helpful.

eelixduppy

3:06 am on Jun 21, 2007 (gmt 0)



hehe, you're welcome. Glad you got everything working :)