Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Will fwrite() save the file immediately?

Tags:

php

file-io

This is my code:

$zplHandle = fopen($target_file,'w');
fwrite($zplHandle, $zplBlock01);
fwrite($zplHandle, $zplBlock02);
fwrite($zplHandle, $zplBlock03);
fclose($zplHandle);

When will the file be saved? Is it immediately after writing to it or after closing it?

I am asking this because I have Printfil listening to files in a folder and prints any file that is newly created. If PHP commits a save immediately after fwrite, I may run into issues of Printfil not capturing the subsequent writes.

Thank you for the help.

like image 837
Nirmal Avatar asked Jul 01 '10 09:07

Nirmal


People also ask

How does fwrite work?

The fwrite function writes up to count items, of size length each, from buffer to the output stream . The file pointer associated with stream (if there's one) is incremented by the number of bytes fwrite writes. If stream is opened in text mode, each line feed is replaced with a carriage return-line feed pair.

What does fwrite return in PHP?

fwrite() function in PHP The fwrite() function writes to an open file. It returns the number of bytes written on success, whereas returns False on failure.

How do I know if PHP fwrite is successful?

fwrite() returns the number of bytes written, or FALSE on error. Check whether fwrite() returns false and you'll know that the write succeeded. Be careful to use the === comparison operator instead of the == operator when checking if the returned value is false.

Does fwrite overwrite?

fwrite() does not exactly overwrite by using fseek(). Instead, there are several cases: if the open permissions was 'r' then writing is an error.


1 Answers

the file will be saved on fclose. if you want to put the content to the file before, use fflush().

like image 105
oezi Avatar answered Sep 23 '22 02:09

oezi