Currently I'm doing it like this:
$f = fopen('test', 'w');
fwrite($f, 'Hi');
fclose($f);
But I have a loop that does some expensive stuff (with files), and I'd like to avoid opening and closing the file handle every time I want to overwrite "test"
It should be something like this:
$f = fopen('test', 'w');
$i = 0;
while($ < 50000){
$i++;
foverwrite($f, 'i = ' . $i);
sleep(5); // just a example
}
fclose($f);
Is this possible?
This script runs in the background in CLI mode and I'm reading the test file from the web with ajax like every 2 seconds. Basically I'm trying to display a progress bar lol
Overwriting a File, Part 1 To edit the settings for a file, locate the file you wish to overwrite and hover over the file name. Click the chevron button that appears to the right of the file name and select Overwrite File from the menu.
To overwrite a file, to write new content into a file, we have to open our file in “w” mode, which is the write mode. It will delete the existing content from a file first; then, we can write new content and save it. We have a new file with the name “myFile. txt”.
Overwriting is the rewriting or replacing of files and other data in a computer system or database with new data. One common example of this is receiving an alert in Microsoft Word that a file with the same name already exists and being prompted to choose whether you want to restore the old file or save the new one.
You can update the content of a file by rewinding the file pointer, see fseek(). Be careful, once you set file lenght to N by writing N bytes to it, next time, if you rewind and write X < N bytes, the file will remain N bytes long, containing X bytes of new data and N-X bytes of old stuff.
Anyway, don't worry about opening and closing files several times, they will be cached. Or you can open it on a ramdisk partition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With