Log in

View Full Version : Mystery error on opening an html file



kuau
03-19-2009, 08:07 PM
I didn't write this code so am not sure why it is getting this error message. What does the '@' sign signify? I don't understand $buffer either. Thanks for any elucidation.


fgets(): supplied argument is not a valid stream resource in /home/public_html/car/php/reserve.php on line 138
[19-Mar-2009 05:04:01] PHP Warning: feof(): supplied argument is not a valid stream resource in /home/public_html/car/php/reserve.php on line 137
-----------------------------------------

$Confirm = $Email1;

@$fp = fopen($Confirm,r);

while(!feof($fp)) { // line 137
$buffer = fgets($fp,100);
$body.= $buffer;
}

fclose ($fp);

Sheppy
03-20-2009, 06:27 AM
Hello kuau and rest!

/firstpost apologies for poor forum etiquette!


I didn't write this code so am not sure why it is getting this error message. What does the '@' sign signify? I don't understand $buffer either. Thanks for any elucidation.

I believe the @ just means it wont throw an error if the variable is undefined. For example:

$query = $_GET["q"];

I will get an error, but if I prefix the variable with the @:

$query= @$_GET["q"];

I wont.

A buffer is like temporary storage collecting data waiting to be sent. In this case its the variable $buffer. You can see while its running feof() its collecting the data in $buffer which is then added onto $body signified by the dot in .=

Try removing the @ sign and seeing what error message you get then.

Hope this helps!

JasonDFR
03-20-2009, 10:27 AM
Short answer is:

$Confirm does not exist.

Long answer is:

When ever dealing with files or resources, it is a good idea to make sure they are valid prior to using them. The code you posted fails to make sure the resource created by fopen is valid before attempting to use it.

Like Sheppy said, the @ suppresses error handling. So if an error occurs, no message is displayed and the script keeps going like nothing bad happened. You want to avoid using @ as much as possible, though in the case of fopen() I can't figure out a great ( simple ) solution that avoids it. If fopen() is only accepting files, and they are on the same server as this script, you could check for the files existence before calling fopen(), by using file_exists().

Knowing what $Confirm could possibly be is important.

Without knowing what $Confirm is or could possibly be, I think this is best:




$fp = @fopen($Confirm, r); // Won't throw an error if fopen can't deal with $Confirm

if ( is_resource($fp) ) { // If fopen() couldn't deal with $Confirm,
// $fp won't be a resource and none of this code
// should be allowed to execute

$body = ''; // Good practice to create a string before adding anything to it

while(!feof($fp)) {

$body .= fgets($fp, 100);

}

fclose ($fp);

} else {

// Problem with fopen()
// $Confirm probably doesn't exist

}