Why do I need `fclose` after writing to a file in PHP?

前端 未结 7 967
囚心锁ツ
囚心锁ツ 2020-12-29 03:44

Why do I need to finish by using the fclose($handle) function after writing to a file using php? Doesn\'t the program automatically do this when it ends?

7条回答
  •  太阳男子
    2020-12-29 04:02

    Yes, PHP normally closes the file before exiting. But you should always close it manually:

    1- It's a good programming practice

    2- PHP can exit unexpectedly (for example, an uncaught exception). This may leave the file with something in the queue to be written, or with a lock in it.

提交回复
热议问题