How to add line to the file without delete - php

I want to write a line to the end of the file.
My code is:
$myFile = "serialkey.txt";
$fh = fopen($myFile, 'w');
$space = "\r\n";
$stringData = "my new data";
fwrite($fh, $stringData.$space);
fclose($fh);
But when I used this code it deleted all the file and replace "my new data", I want it will not delete my file and append my data to it.

You have it set to write instead of append in
$fh = fopen($myFile, 'w');
It should be
$fh = fopen($myFile, 'a');
Hope this helps.

To insert text without over-writing the end of the file, you'll have to open it for appending (a+ rather than w)
$fh = fopen($myFile, 'a+');

Related

Code is only saving the last entry of the loop

I want to filter one text file by writing the results to a second text file.
I have a little bit of code and it doesn't work like it should work, it is only writing the LAST line of the first text file into a separate backup text file.
Code:
//filter the ingame bans
$search = "permanently";
$logfile = "ban_list.txt";
$timestamp = time();
// Read from file
$file = fopen($logfile, "r");
while( ($line = fgets($file) )!= false)
{
if(stristr($line,$search))
{
$cache_ig = "ingamebanlist.txt";
$fh = fopen($cache_ig, 'w') or die("can't open file");
$content = "\n";
fwrite($fh, $content);
$content = $line;
fwrite($fh, $content);
fclose($fh);
}
}
I personally do not see any errors in my code, please help.
REMEMBER: It does kind of work, but it only writes the LAST line of the ban_list.txt file into the ingamebanlist.txt file...
What happens with your code is that you open(using write mode), write and close inside your loop so it will only ever write 1 entry then overwrite it until the last entry, thus only saving the last item.
What you want is to have it outside the loop like this:
<?php
$search = "permanently";
$logfile = "ban_list.txt";
$cache_ig = "ingamebanlist.txt";
$timestamp = time();
$read = fopen($logfile, "r") or die("can't read file");
$write = fopen($cache_ig, 'w') or die("can't write to file");
while(($line = fgets($read)) !== false)
{
if(stristr($line,$search))
{
fwrite($write, $line . "\n");
}
}
fclose($write);
fclose($read);
An alternative solution would be to use a instead of w at your fopen since w will:
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.
While a will:
Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
Which would allow your code to work as is without any changes but the line:
$fh = fopen($cache_ig, 'w') or die("can't open file");
To:
$fh = fopen($cache_ig, 'a') or die("can't open file");

How can I keep previous data in a text file?

So basically, I have a form that opens a php file when submitted, i have the php writing to a file, but it will not continue adding values, the text file only has one "1" inside of it when it is supposed to have a "1" inside for every time the form has been submitted. Here is my code.
<?php
$myFile = "Data.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "1";
fwrite($fh, $stringData);
fclose($fh);
?>
Any ideas?
Either open in append only format as Robert Rozas stated: fopen($myFile, 'a+');
Or get the contents and do the append manually some pseudo[esk]-code:
$contents = file_get_contents("somefile.txt");
//generate what to write
$contents .= $whatIGenerated;
$success = file_put_contents("somefile.txt", $contents);
Change this one line to this:
$fh = fopen($myFile, 'a') or die("can't open file");
Using w means write, using a means append.

How to inform the user when php is done saving the file?

after suggestions in this post, I finally made my code worked.
$myFile = "data.txt";
$fh = fopen($myFile, 'w');
$x = $result . "\r\n";
fwrite($fh, $x);
fclose($fh);
PHP is sequential. If you tell it to save to a file and then echo afterwords it will do exactly that. It will finish saving to the file and then echo afterwords. But I must ask - could you be executing another separate PHP file inside of your submit function? If so then pseudo code above could very well echo out before the background process of saving the file is completed...
after suggestions in this post, I finally made my code worked.
$myFile = "data.txt";
$fh = fopen($myFile, 'w');
$x = $result . "\r\n";
fwrite($fh, $x);
echo "Done Saving!";
fclose($fh);

Is it possible to do "if file exists then append, else create new file" shorter than this

I have the following code but I'm trying to shorten it about one or two lines, as I believe the if is unnecessary. Is there any way the code below can shortened to a singular line?
if(file_exists($myFile))
{
$fh = fopen($myFile, 'a');
fwrite($fh, $message."\n");
}
else
{
$fh = fopen($myFile, 'w');
fwrite($fh, $message."\n");
}
if (file_exists($myFile)) {
$fh = fopen($myFile, 'a');
fwrite($fh, $message."\n");
} else {
$fh = fopen($myFile, 'w');
fwrite($fh, $message."\n");
}
fclose($fh);
==
if (file_exists($myFile)) {
$fh = fopen($myFile, 'a');
} else {
$fh = fopen($myFile, 'w');
}
fwrite($fh, $message."\n");
fclose($fh);
==
$fh = fopen($myFile, (file_exists($myFile)) ? 'a' : 'w');
fwrite($fh, $message."\n");
fclose($fh);
== (because a checks if the file exists and creates it if not)
$fh = fopen($myFile, 'a');
fwrite($fh, $message."\n");
fclose($fh);
==
file_put_contents($myFile, $message."\n", FILE_APPEND);
...of course, file_put_contents() is only better if it is the only write you perform on a given handle. If you have any later calls to fwrite() on the same file handle, you're better going with #Pekka's answer.
Umm... why? a already does what you need out of the box.
Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
$method = (file_exists($myFile)) ? 'a' : 'w';
$fh = fopen($myFile,$method);
fwrite($fh, $message."\n");
fopen(). mode a all you need.
$fh = (file_exists($myFile)) ? fopen($myFile,'a') : fopen($myFile,'w');
fwrite($fh, $message."\n");
$fh = file_exists($myFile) ? fopen($myFile, 'a') : fopen($myFile, 'w');
fwrite($fh, $message."\n");
The append mode already does just what you describe. From the PHP manual page for fopen:
'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.
According to the php manual this should be enough. See the description of "a"
fopen($myFile, "a");
fwrite($fh, $message."\n");
I believe the a (append) mode does that already... append if exists, else create new
fopen($myFile, "a");
$method = (file_exists($myFile)) ? 'a' : 'w';
$fh = fopen($myFile,$method);
fwrite($fh, $message."\n");
Isn't it $myFile contains absolute/relative path..?
Using the SPL / Standard PHP Library:
# addfile.php
$file = new \SplFileObject( __DIR__.'/foo.txt', 'a' );
var_dump( file_exists( $file->getFilename() ) );
$ php addfile.php
bool(true)

How to specify a location on appending a file

$myFile = "testFile.txt";
$fh = fopen($myFile, 'a');
I want the testFile.txt to be located at the Desktop, and i perform this at C:/wamp/localhost/
How could state the location or can is it possible?
Please help.
You can do:
$myFile = 'C:\Documents and Settings\<yourusername>\Desktop\testFile.txt';
i think you want to say how to open your text file for append. then
$myFile = "YOUR_DESKTOP_PATH/testFile.txt";
$fh = fopen($myFile, 'a');

Categories