Searching filenames in a directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lukeprice
Forum Newbie
Posts: 2
Joined: Tue Jun 03, 2008 3:01 am

Searching filenames in a directory

Post by lukeprice »

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:

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);
 
?>
 
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
User avatar
vargadanis
Forum Contributor
Posts: 158
Joined: Sun Jun 01, 2008 3:48 am
Contact:

Re: Searching filenames in a directory

Post by vargadanis »

Hi!

What I would do is adding a new array in front of the while loop. When a color is found I would add it's name or data or whatever to the array. In the end I would just list the content of the array.

I don'1 know what are the true.php and false.php files but it is usually better not to include a source file somewhere in the middle.
lukeprice
Forum Newbie
Posts: 2
Joined: Tue Jun 03, 2008 3:01 am

Re: Searching filenames in a directory

Post by lukeprice »

Hi, could you please explain how to do this, thanks.
User avatar
vargadanis
Forum Contributor
Posts: 158
Joined: Sun Jun 01, 2008 3:48 am
Contact:

Re: Searching filenames in a directory

Post by vargadanis »

You can create an empty array with

Code: Select all

$array_to_display = array();
If you want to push data into it you need to use

Code: Select all

array_push($array_to_display, $data);
If you want to list the contents of an array:

Code: Select all

foreach ( $array_to_display as $k=>$v ) {
   print "<pre>";     // The following is a useful debugging method, learn it!
   print_r($v);       // this will display the entire content of $v
   print "</pre>";
}
 
User avatar
vargadanis
Forum Contributor
Posts: 158
Joined: Sun Jun 01, 2008 3:48 am
Contact:

Re: Searching filenames in a directory

Post by vargadanis »

[ OFF ]
And one more thing. I use NetBeans IDE for writing PHP apps. Under linux you can have multiple desktops. I have php.net opened on the 1st one, the output in FireFox on the 3rd one and Netbeans on the second one. You can switch between desktops a lot faster than Windows. :)
I wrote it to you just to let you know how I do it ( I found this the most effective esp. the php.net part of it where I can find reference to everything related to PHP :) )
[ /OFF ]
Post Reply