andrewniesen
andrewniesen

Reputation: 394

fwrite only works once in PHP

I'm running into a strange problem migrating an app to a new LAMP server. I have a PHP script that writes multiple files each time it is executed. After numerous problems, I've written some code to test how fwrite() is working on this server.

Here is what I have found:

When executing the script with this code:

$fh=fopen($dir.'/log.txt','w');
fwrite($fh, $log);
f_close($fh);

$fh2=fopen('/tmp/log.txt','w');
fwrite($fh2, $log);
f_close($fh2);

A file called log.txt is created in $dir but not in /tmp

However, when executing this code:

//$fh=fopen($dir.'/log.txt','w');
//fwrite($fh, $log);
//f_close($fh);

$fh2=fopen('/tmp/log.txt','w');
fwrite($fh2, $log);
f_close($fh2);

the log.txt file IS created in /tmp (but, of course, NOT in $dir)

I've tried a few other iterations as well, and it appears that fwrite() (or perhaps fopen()) only works the first time it is invoked; it's ignored each subsequent time.

Any ideas or suggestions as to why this might be happening?

Upvotes: -1

Views: 752

Answers (1)

Vyktor
Vyktor

Reputation: 21007

There's no function f_close() in, you probably meant to use fclose().

And calling an undefined function results into fatal error and your script terminates (second block is never executed).

If you turn on logging/error displaying you should be able to see error message like:

Fatal error: Call to undefined function f_close() in script.php on line 123

Upvotes: 1

Related Questions