Update/Modify existing file on server - php

I want to add content to file on every page load, but it overwrite file, not updating content. my original code:
$file_handle = fopen('log.txt', 'w');
fwrite($file_handle, $message);
fclose($file_handle);
After i searching, nothing found to solve my prolem:
How to edit/update a txt file with php
PHP Modify a single line in a text file
I used w+ and r+ but it didn't work.
$file_handle = fopen('log.txt', 'w+');
fwrite($file_handle, $message);
fclose($file_handle);
Both write file with new content, not keep old contents. I want to keep old contents, just append new content to existing file.

You are trying to append to a file so you need the mode 'a':
$file_handle = fopen('log.txt', 'a');
fwrite($file_handle, $message);
fclose($file_handle);
From the docs:
'a': Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() has no effect, writes are always appended.

Related

how to write into a text file in php without delete all the data

how can I write into text file without erase all the existing data?
I tried this
$txt = 'srge';
$file = fopen('text.txt','w');
fwrite($file,$txt);
but it's not working, it's earse everything
Note: This will only work when you have appropriate permission for test.txt else it will say
permission denied (un-appropriate will lead to this)
Here we are using:
1. a which is for append this will append text at the end of file.
2. instead of w, flag w is for write, which will write on file without caring about you existing data in that file.
PHP code:
<?php
ini_set('display_errors', 1);
$txt = 'srge';
$file = fopen('text.txt','a');
fwrite($file,$txt);
according to php documentation:
while you are using :
'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.
try instead:
'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() has no effect, writes are always appended.
Try with following code
$txt = 'srge';
$file = fopen('text.txt','a');
fwrite($file,$txt);
Writing or Appending to a File
The processes for writing to or appending to a file are the same. The difference lies in the fopen() call. When you write to a file, you should use the mode argument "w" when you call fopen():
$fp = fopen( "test.txt", "w" );
All subsequent writing will occur from the start of the file. If the file doesn't already exist, it will be created. If the file already exists, any prior content will be destroyed and replaced by the data you write.
When you append to a file, you should use mode "a" in your fopen() call:
$fp = fopen( "test.txt", "a" );
For more details please refer this : File operation example
Php Filesystem Functions
You can use this.
$content = 'any text';
$file = fopen('file.txt','a');
fwrite($file,$content);
Have you noticed i used mode a
"a" (Write only. Opens and writes to the end of the file or creates a new file if it doesn't exist)

PHP Understanding stream_get_contents on existing file opened with w+

I was trying to figure out file pointers in PHP, so I wrote a small script to test it out:
$fp = fopen('test.me', 'w+');
fwrite($fp, '12345');
rewind($fp);
echo 'contents: ['.stream_get_contents($fp)."]\n";
fseek($fp, 0, SEEK_END);
fwrite($fp, '6789');
fclose($fp);
echo 'contents: ['.file_get_contents('test.me')."]\n";
Nice and easy. Open a file to read/write from it. Write to it. Go back to the beginning of the file, print out its contents. Go to the end of the file, append some more to it. Finally, print out the entire contents of the file.
I run it and get the exact results that I expect:
contents: [12345]
contents: [123456789]
Being so proud of myself for writing this masterpiece, I go to my terminal and hit up and enter to run it again.
This time:
contents: []
contents: [123456789]
Turns out that if the file exists at the start of the script, the first block is always contents:[]. If I delete the test.me file, then the first block is contents:[12345].
What's going on here?

opening a .txt file with php (read / write) and deleting the old content and replacing it with a new one

I want to change the old values of a text file with a new values (delete the old content and replace it), when I use the code below it shows me an error page , don't really know how to fix this , even used the different types of file opening methods (w , r+ , a ...) and didn't work !
$i=$_POST['id'];
$fp = fopen ("/test/member_dl.txt", "r+");
$ch = fgets ($fp);
$t=explode('||',$ch);
$t[$i-1]='';
$ch2=implode('||',$t);
fwrite($fp,$ch2);
fclose ($fp);
As you want to completely replace the contents, why not just delte it, and re-create it?
unlink ("/test/member_dl.txt");
$fp = fopen ("/test/member_dl.txt", "r+");
// Continue with your code.
// Not sure I follow what you are doing with it
Edit: I am not sure I understand what that part of your code is doing to be honest.
The unlink() command deletes the file. From there you can start over and write the file out as you need it?
Whilst it's open as $fp = fopen ("/test/member_dl.txt", "r+");
You will not be able to fwrite($fp,$ch2);
Opening it with 'w+' should enable read and writing.
Try this:
$i=$_POST['id'];
$fp = fopen("/test/member_dl.txt", "w+");
$ch = fread($fp, filesize($fp));
$t=explode('||',$ch);
$t[$i-1]='';
$ch2=implode('||',$t);
fwrite($fp,$ch2);
fclose ($fp);
EDIT:
Tested this, this works
$ch = file_get_contents("test.txt");
$t=explode('||',$ch);
$t[$i-1]='';
$ch2=implode('||',$t);
file_put_contents("test.txt","hello");

Why PHP file handler doesn't work properly?

I have this content on 'test.txt' file: lucas
I want to seek pointer in the file and override info ahead. Supposed I do:
$f = new SplFileObject('test.txt', 'a');
$f->fseek(-5, SEEK_END);
var_dump($f->ftell());
$f->fwrite('one');
This should produce: oneas But the result of execution: lucasone
I'm crazy about the code logic or even doesn't works?
How is the right way to do what I want?
You opened the file for appending:
$f = new SplFileObject('test.txt', 'a');
which means you cannot seek in the file. Instead, open it for reading and writing:
$f = new SplFileObject('test.txt', 'r+');
They also say it in the fseek documentation:
If you have opened the file in append (a or a+) mode, any data you write to the file will always be appended, regardless of the file position, and the result of calling fseek() will be undefined.

Can this fopen code be improved

I'm seeing that this code first creates the file, closes it, then opens it with 'a', writes to it, then closes it. Is there a way to simplify it. The idea is that if the file name exists, it needs to be overwritten. I also don't understand the point of unset. Is it necessary?
$fp = fopen($file_name, 'w');
fclose($fp);
unset($fp);
$fp = fopen($file_name, 'a');
fputs($fp, "sometext");
fclose($fp);
unset($fp);
From php.net, under the 'w' mode in fopen: 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.
In other words, open for writing, and overwrite or create as necessary. No need to use append mode.
$fp = fopen($file_name, 'w');
fputs($fp, "sometext");
fclose($fp);
file_put_contents($file_name, 'sometext');
And, No, unset() is pointless in your case.

Categories