How to clear text file contents in php

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

I have a bash script that runs a php file on the command line and sends the results to a text file. for the β€œnext” run of the bash script, I want to clear the previous results from the result file.

0
source share
3 answers

@Straight. A simple cp /dev/null yourfile in your bash script will take care of this.

0
source

file_put_contents will do this.

 file_put_contents('text.txt', null); 

Of course, you can also destroy the contents of a file by redirecting it to it and writing something from bash.

+4
source

You can also truncate the file to a specified size by following these steps:

 ftruncate(fopen('text.txt'), 0); 

This example will clear it, but 0 can be anything in the range [0 - file size].

+1
source

Source: https://habr.com/ru/post/885836/


All Articles