[thelist] PHP includes/comparing string values

Timothy Martens timfm at hawaii.rr.com
Mon Mar 4 18:57:00 CST 2002


Andrew Forsberg wrote:

"
That would be because $content is NULL. NULL won't be equal to your
$checkval string, or not equal to it. Try separating the code to do with
reading the file from the rest, and make sure that works properly first.
The filemode used with fopen(), "w" is write only, so fread() isn't
going to return any value... except NULL. Try using "r+" instead -- that
opens the file for reading and writing, and puts the pointer at the
start of the document. Once the file is read you'll need to do a little
more fiddling in order to write over the top of it again, but the docs
should help, otherwise just shout out here! :)
"

Thanks for the feedback Andrew!

I've gotten somewhere, but something funny is still happening. I messed
around with reading the $contents and then echoing them which helped.
Then tried fseek to get the cursor in the right position for overwrite
but that was not working as I had hoped (maybe 'rewind'?) so I now have
this:

<?
$noting = "TESTING";
$checkval = "<div style='color: #F00; font: bold 80px Georgia,
serif'>Hello World</div>";
$filename = "thefile.inc";
$fd = fopen ($filename, "r");
$contents = fread ($fd, filesize ($filename));
$fw = fopen ($filename, "w");

if ($contents == $checkval) {
    fclose ($fd);
    fwrite ($fw, $noting);
    fclose ($fw);
    include ($filename);
    } else {
    fclose ($fd);
    include ($filename);
}
clearstatcache();
?>

So it sort of works. I mean when the $contetns == $checkval -- it does
overwrite the $filename with return $noting/"TESTING"

BUT... When I refresh/comeback to the page. That is, when $contents and
$checkval are no longer equal, rather than just including $noting, it
seems to be overwriting "thefile.inc" altogether with NOTHING?

Cheers,

//t




More information about the thelist mailing list