Re: fgets fails after fopen succeeds

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, May 10, 2005 1:34 pm, Thomas Powell said:
> Summary:
> fopen returns a file handle, yet fgets doesn't want to read it. I've
> scoured
> google, php.net <http://php.net>, phpfreaks, usenet, etc. It seems to work
> elsewhere.
>
> My setup:
> Apache 2.0.47
> PHP/5.0.4
> Windows 2000
>
> The call:
> make_link_list("index");
>
> (the file exists for fopen(), and actually errors if I add characters to
> break it)
>
> My Source Code:
>
> <?php
> function make_link_list($name="") {
>
> print $_SERVER['DOCUMENT_ROOT'].'/outside/'.$name.'.txt';
> ?>
> <ul>
> <?
> $fh=fopen($_SERVER['DOCUMENT_ROOT'].'/outside/'.$name.'.txt', 'r+') ||
> die("Cannot open file");

I don't think you want || here -- The operator precedence of = and || and
OR are what's biting you in the butt, almost for sure.

echo "fh is $fh<hr />\n";

If that doesn't print out "Resource #1" (or some other number) then you've
got it wrong, and you might as well ignore the rest of this script until
it's right.

Also, it's probably REALLY BAD SECURITY that the PHP user can write (r+) a
file inside your DOCUMENT_ROOT!!!

Move the file somewhere else, outside the web tree, and give yourself a
config file to set the path for easy changing/installing.


> for ($line = fgets($fh, 1000); 0 && ! feof($fh); $line = fgets($fh)) {

Dunno what the 0 && is about, but it don't belong there when you're done,
of course...

>  $line = trim($line);
>  if(strlen($line) > 0) {
>  print '<li>' . $line ."</li>\n";
>  }
> }
> ?>
> </ul>
> <?
> fclose($fh);
> }
> ?
>
> Output:
> C:/Program Files/Apache Group/Apache2/htdocs/youmightbe/outside/index.txt
>
>
>    *Warning*: fgets(): supplied argument is not a valid stream resource
>    in *C:\Program Files\Apache
>    Group\Apache2\htdocs\youmightbe\outside.php* on line *11*
>
>
> *Warning*: fclose(): supplied argument is not a valid stream resource
> in *C:\Program
> Files\Apache Group\Apache2\htdocs\youmightbe\outside.php* on line *20*
>


-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux