Code Library - php
Directory Search - (1651 views)
<!-- usage: script.php?string=stringtosearch -->
<?
$string = $_GET['string'];
//full path to the directory
$path = "/home/me/public_html/dir/";
$fp = @opendir($path);
//check we have a string to search
if ($string && $string != "") {
while (($file = readdir($fp)) !== FALSE) {
//match the string with any files in the directory
if (preg_match('/'.$string.'/', $file)) {
$match = "1";
//strip of the extension and return just the filename
$ext = explode('.', $file);
$file_ext = strtolower(".". $ext[1]);
$filename = str_replace("$file_ext","",$file);
//show the results
echo "<a href=\"$file\">$filename</a><br>";
}
}
closedir($fp);
}
else {
//if we don't have a string or it's blank
echo "we don't have a search string";
$match = "1";
}
//if we didn't mark a match, there's no results
if (!$match) {
echo "no results for $string";
}
?>