PHP: Is there a command that can delete the contents of a file without opening it?

前端 未结 6 1760
面向向阳花
面向向阳花 2020-12-07 03:20

Is there any way to remove the contents of an file in php, do we have any php command that does that, I know unlink but I do not want to delete the file instead

6条回答
  •  囚心锁ツ
    2020-12-07 03:54

    I don't know have you write the contents into the file, but if you use fopen and you empty the file first, you are basically doing what the w mode is doing anyway.

    From the documentation:

    'w': Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

    So I really see no need in emptying it first as you probably will use the w mode anyway.

    You should only empty it if your code relies on the fact that this file is empty or if it is somehow security relevant.

提交回复
热议问题