W3docs

Clearing content of text file using php

To clear the contents of a text file using PHP, you can use the file_put_contents() function.

To clear the contents of a text file using PHP, you can use the file_put_contents() function. This function writes a string to a file. If the file does not exist, it will be created. If the file already exists, it will be truncated (its contents will be deleted).

Here is an example of how you would use the file_put_contents() function to clear the contents of a text file called "example.txt":

Example of using the file_put_contents() function to clear the contents of a text file in PHP

php— editable, runs on the server

This will clear the contents of "example.txt" and will write an empty string to the file.

You can also use fopen, ftruncate and fclose to clear the contents of a text file

Example of using fopen, ftruncate and fclose to clear the contents of a text file in PHP

php— editable, runs on the server

This will open the file in "write" mode, truncate it to zero bytes and close it.