Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i test if a file write has succeeded in PHP?

Tags:

php

Is there any way to test if writing to a file was successfully accomplished? I need a method to obtain the end of time of a writing operation. If so to trigger a callback function.

like image 928
Endre Simo Avatar asked Sep 17 '12 13:09

Endre Simo


People also ask

How to check if fwrite was successful?

The fwrite() function returns the number of members successfully written, which may be less than nitems if a write error is encountered. If size or nitems is 0, fwrite() returns 0 and the state of the stream remains unchanged.

How to include file path in PHP?

PHP Include Files. The include (or require ) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement. Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.


1 Answers

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.

You can use filemtime() to get the last modification time of a file.

like image 69
Samuel Avatar answered Oct 19 '22 18:10

Samuel