Searching filenames in a directory
Posted: Tue Jun 03, 2008 3:17 am
Hi,
Hope someone can help me. New to PHP and struggling a bit with this problem.
I want to search all filenames in a directory and compare them to a string input into a form by the user. The files are all colour references so if I want to search for colour reference '003', I want to be able to type 003 into the form and then PHP to search the specified directory for any filenames that match '003' then show a hyperlink to this file to the user, or alternatively display a message saying 'No colours found'.
I have made a bit of progress but I can't get past a problem that the script is running over every file in the directory individually so I am getting some 'false' returns and some 'true'. This means even if the search string is matched bya filename I am still getting 'No colours found' along with the matched colour. Anyway, here is the code:
And here is a 'working' demo (the only searchable colour refs atm are 002, 003 and 004): http://www.daedalian-glass.co.uk/test_a ... ol/002.htm
I am pretty sure I am going about this the wrong way and im not even sure if strpos is the best search method to use, I looked into preg_match but it went over my head a bit, would this be more suitable?
Any help would be greatly appreciated, I haven't got time to enter all the colours into a MySQL DB and I understand that would probably be the best way to do this but I just don't have the time.
Thanks
Hope someone can help me. New to PHP and struggling a bit with this problem.
I want to search all filenames in a directory and compare them to a string input into a form by the user. The files are all colour references so if I want to search for colour reference '003', I want to be able to type 003 into the form and then PHP to search the specified directory for any filenames that match '003' then show a hyperlink to this file to the user, or alternatively display a message saying 'No colours found'.
I have made a bit of progress but I can't get past a problem that the script is running over every file in the directory individually so I am getting some 'false' returns and some 'true'. This means even if the search string is matched bya filename I am still getting 'No colours found' along with the matched colour. Anyway, here is the code:
Code: Select all
<?php
$path = "rosco_e_col";
$dir_handle = @opendir($path) or die("Error opening $path");
while ($file = readdir($dir_handle)) {
if($file == "." || $file == ".." || $file == "col_list.txt" ) { continue; }
$colour = str_replace(".htm", "", $file);
$search = $_POST['id'];
$match = strpos($colour, $search);
if ($match !== false) {
include_once ('true.php');
$pos = $colour;
echo $pos;
} else {
include_once ('false.php');
}
}
closedir($dir_handle);
?>
I am pretty sure I am going about this the wrong way and im not even sure if strpos is the best search method to use, I looked into preg_match but it went over my head a bit, would this be more suitable?
Any help would be greatly appreciated, I haven't got time to enter all the colours into a MySQL DB and I understand that would probably be the best way to do this but I just don't have the time.
Thanks