Click to See Complete Forum and Search --> : feof() error


Flying Monkey
01-12-2007, 08:27 PM
Hi. Please forgive my ignorance. I am teaching myself php and mysql with the help of Luke Welling and Laura Thomson's "PHP and MySQL Web Development. I have been getting this error:

Warning: feof(): supplied argument is not a valid stream resource

which loops down the browser page. I've tried over and over but I can't find my syntax error and I've contacted my server administrator who says file functions should be operational. Heres the code in question:

<?php
$order = 0;
@ $fp = fopen("DOCUMENT_ROOT/../orders/orders.txt", 'r');

if (!fp) {
echo '<p><strong>No orders pending.</strong></p></body></html>';
exit;
}

while (!feof($fp)) {
$order = fgets($fp, 999);
echo $order.'<br />';
}

fclose($fp);

?>

orders.txt is sitting in a folder called orders outside the ROOT folder
Any help is greatly appreciated.
Monkeyman

NogDog
01-12-2007, 08:48 PM
$fp = fopen($_SERVER['DOCUMENT_ROOT']."/../orders/orders.txt", 'r');

NogDog
01-12-2007, 08:54 PM
PS: to be a bit more robust:

<?php
$order = 0;
$file = $_SERVER['DOCUMENT_ROOT']."/../orders/orders.txt";
if(file_exists($file))
{
$fp = @fopen($file, 'r');
if (!fp)
{
user_error("Unable to open orders file, check file permissions", E_USER_WARNING);
}
else
{
while (!feof($fp)) {
$order = fgets($fp, 999);
echo $order.'<br />';
}
fclose($fp);
}
}
else
{
echo '<p><strong>No orders pending.</strong></p></body></html>';
exit;
}
?>

Flying Monkey
01-12-2007, 10:25 PM
Hi NogDog, thanks for your response - it now works!

I actually did declare the DOCUMENT_ROOT as:

$DOCUMENT_ROOT = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];
before the <html> doctype declaration,
but I don't think it as that. was it the "@" before the "fp" variable declaration?

$fp = fopen("DOCUMENT_ROOT/../orders/orders.txt", 'r');

NogDog
01-12-2007, 10:49 PM
If that's the case, then you left the "$" out before the variable name:

$fp = fopen("$DOCUMENT_ROOT/...etc...

Flying Monkey
01-12-2007, 11:02 PM
muchos grazias DogNogs

NightShift58
01-13-2007, 02:51 PM
muchos grazias DogNogsA couple of mistakes here...

(1) "grazias" should be "gracias".
(2) "muchos" should be "muchas" - as "gracias" is feminine in Spanish.

But this is very minor stuff.

Unforgivable, however, is "DogNogs"... It's "NogDog" and that's what he looks like (http://www.webdeveloper.com/forum/showthread.php?p=690740#post690740) - so tread carefully...

And, of course: :)