Forum Moderators: coopster

Message Too Old, No Replies

Searching Directory Files Pt 2

         

unclekracker23

9:51 pm on Aug 1, 2007 (gmt 0)

10+ Year Member



Hello guys, quick question. I am trying to add an else command to the following code to where if they enter the wrong filename or the name to a file that does not exist then they will get a message that says try again or whatever.

Currently the script works great when the correct name is entered but is blank when incorrect name is entered.

Here's the code...

<?php
$dir = "test";
$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 "<b><u>Search Results</u></b>";
echo "<br><br>";
echo "<a href='$file' target='_blank'>$file</a>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<a href='./'>Go Back to Search</a>";

}
}
closedir($res);
?>

unclekracker23

11:43 pm on Aug 1, 2007 (gmt 0)

10+ Year Member



BTW, I have tried tons of ways to get this to work and can't. This is why I posted the proper working code above as to have a fresh starting point.

unclekracker23

8:26 pm on Aug 2, 2007 (gmt 0)

10+ Year Member



Can anyone please help? I realy need to get this working.

Every option I have tried doesnt work. It will either print "Try again" whether it finds the file or not or it will print "Try Again" for every file it checks in the directory (44 files, 44 "Try Agains").

jatar_k

1:05 am on Aug 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you can't really add an else to a while loop but you can add a var to test at the end

<?php
$dir = "test";
$exclude = array('.','..','.htaccess');
$q = (isset($_GET['q']))? strtolower($_GET['q']) : '';
$res = opendir($dir);
$foundfile = false;
while(false!== ($file = readdir($res))) {
if(strpos(strtolower($file),$q)!== false &&!in_array($file,$exclude)) {
echo "<b><u>Search Results</u></b>";
echo "<br><br>";
echo "<a href='$file' target='_blank'>$file</a>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
echo "<a href='./'>Go Back to Search</a>";
$foundfile = true;
}
}
closedir($res);

if (!$foundfile) {
echo 'you blew it!';
}
?>

should work

unclekracker23

1:50 am on Aug 3, 2007 (gmt 0)

10+ Year Member



jatar, thanks! I will test it and see how it works. This has been driving me nuts as I am a complete newb. I can't wait to get into some classes locally.

unclekracker23

2:27 am on Aug 3, 2007 (gmt 0)

10+ Year Member



jatar, Works perfectly! Thank you soo much.

Edit: I had actually came close to having that myself a couple times. I had a couple variables in the wrong line.

jatar_k

3:03 am on Aug 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



no problem

just keep plugging away, it'll come and we'll help as much as we can