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.
Related
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.
Ok so I am new to PHP and the example from the book shows that when I write to a file in php i should use flock($fp, LOCK_EX) after opening the file and then flock($fp, LOCK_UN) before closing the file. Like this:
$fp = fopen("$DOCUMENT_ROOT/order.txt", 'ab');
flock($fp, LOCK_EX);
fwrite($fp, $outputstring, strlent($outputstring));
flock ($fp, LOCK_UN);
fclose($fp);
But in the other example of how to read a file I am not sure if the author forgot to put flock() or it is not necessary to put flock() when reading a file. Here is the example:
$fp = fopen("$DOCUMENT_ROOT/order.txt", 'rb');
while (!feof($fp))
{
$order = fgets($fp, 999)
echo $order."<br/>";
}
fclose($fp);
So should I put flock() in the second example or not?
Thank you in advance.
It is only necessary to use flock if there are going to be multiple processes accessing the file at the same time.
If the code that writes to the file is only ever going to have have one copy running at any time, then you probably don't need to use flock. However if it is possible that multiple processes could try to run the code (and therefore access the file) at the same time, then you should use flock to make sure they do it one at a time.
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");
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.
I'm creating a class that will store data in a file and I need to know if it's possible to do multiple read/writes using one file handle?
I've done some experiments, and using this code:
$file_name = 'test.txt';
$file = fopen($file_name, 'r+');
echo fread($file, filesize($file_name)) . "<hr />";
fwrite($file, "|".rand());
echo fread($file, filesize($file_name));
fclose($file);
The file gets read the first time, the write works, but the second fread is blank.
Using this code:
$file_name = 'test.txt';
$file = fopen($file_name, 'r+');
echo fread($file, filesize($file_name)) . "<hr />";
fwrite($file, "|".rand());
fclose($file);
$file = fopen($file_name, 'r+');
echo fread($file, filesize($file_name));
fclose($file);
The second fread then works, but it doesn't contain the data that was appended to it in the fwrite just above it.
So basically, I want to create a single file handle as a property of the class, and have it's methods refer to it when reading and writing, but including any changes that other methods might've made to the data.
I do have 2 alternatives if this isn't possible, one being opening and closing the file handle in each method but this seems really inefficient and I'm not sure what a single file open/close 'costs' in terms of performance. The other alternative is that the data is being converted into array for easy manipulation, I could update the array rather than the file every time but then when would I update the file?
Check out fseek to move your pointer within the file and read from there, fread and fwrite are moving that pointer forward.
And you have to use a+ instead of r+, see: the mode of fopen