Click to See Complete Forum and Search --> : Variable problem... IF


deep
08-25-2003, 12:32 PM
I feel like exploading when a simple code like this fu*ks up... Im compering 2 variables here and in this case the if - statement never goes true... (the one I marked below)

WHY?

The $var variable is located on another page as a textfield and sent here through the form after the user press submit.

This script is seposed to extract a word on the forst line in one file and compare it with another given thriugh a form submit (textfield).


<?php

if (file_exists("word.txt")) {

if (!$fp = fopen("word.txt", "r")) { echo "Error!"; }
$fil_pass = fgets($fp, 15);
fclose($fp);

#
if ($var == ""){

echo "You didnt enter a word!";

#
} elseif ($var == $fil_pass) {

echo "words match!!"; # THE WORDS MATCH BUT STILL I GET "Words did not match!"!!????

#
} else {

echo "Words did not match!";

}

#
} else {

echo "The file words.txt does not exist!";

}

?>

pyro
08-25-2003, 12:37 PM
I'm guessing the line from the file contains a \n (or \r\n on Windows). Try running $fil_pass through rtrim (http://us2.php.net/rtrim)...

deep
08-25-2003, 12:59 PM
You're the best man!

One more question :) :

I got a folder called 'users'. How can I print all the names of the files on the screen...??

You know:

echo $userfiles;

:D

Thx

pyro
08-25-2003, 01:23 PM
Try this:

<?PHP

$folder = "users/";

$handle = opendir($folder);
# Making an array containing the files in the current directory:
while ($file = readdir($handle))
{
$filename[] = $file;
}
closedir($handle);

for ($i = 0; $i < sizeof($filename); $i++) {
if ($filename[$i] == "." || $filename[$i] == "..") {
}
else {
echo "$filename[$i]<br>\n";
}
}

?>