[thelist] Re: PHP includes/comparing string values - SOLUTION

Timothy Martens timfm at hawaii.rr.com
Tue Mar 5 17:00:01 CST 2002


Andrew Forsberg wrote...
"
Try not opening the "w" connection until you have to. Opening it with
"w" will erase everything in the file -- as you've seen :-). In
pseudo code you want to:

open the file (read only)
read the file contents into a string
close the (read only) file
compare the contents string and your check value
if they're equal
  - open a write connection
  - write the value
  - close the connection
  - include the file
else, if they're not equal
  - include the file
So, you do your read stuff first, then, if you need to overwrite the
file you open a "w" connection, otherwise you don't need to fopen() a
write connection at all.
"

Hi Andrew -- yes that did it. I have it working with:

$noting = "TEST TEST";
$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));
fclose ($fd);

if ($contents == $checkval) {
    $fw = fopen ($filename, "w");
    fwrite ($fw, $noting);
    fclose ($fw);
    include ($filename);
    } else {
    include ($filename);
}
clearstatcache();

I appreciate you "spelling" it out without actually writing the code. It
helped me understand the solution. I guess part of my problem was that I
didn't realize that the variable declarations above the if/else were
actually executed without being called! ANY comments as to how to make
this more elegant would be appreciated.

Thanks again	//t




More information about the thelist mailing list