Forum Moderators: coopster
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
<?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?
<?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);
}
}
?>
<?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);
}
}
?>
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
$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!
$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.
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