php fwrite() reports characters written, but they're not there - php

I provide this directory & file:
$path = $_POST['q2Path'];
$file = "test.txt";
// q2Path == "C:\Users\micah\Desktop\only_dir_named_this"
The code runs this block:
$write_str = "TEST TEST TEST";
$fh = fopen($file, 'a') or die("can't open: $path\\$file");
$chars = fwrite($fh, $write_str) or die("can't write to: $path\\$file");
fclose($fh) or die("can't close: $path\\$file");
echo "<pre>> Appended $chars characters to: $path\\$file</pre>";
This is the browser output:
Appended 14 characters to: C:\Users\micah\Desktop\only_dir_named_this\test.txt
And in the end, the file is empty and the 'last modified' timestamp hasn't changed. I'm not sure what's up. I'm using ZendServer on Win7. Perhaps there's a php.ini setting that needs adjustment? I've never had a problem with a simple file write...

You write $path\\$file, but you do fopen($file, 'a')
It should be
$fh = fopen($path . DIRECTORY_SEPARATOR . $file, 'a') or die("can't open: $path\\$file");

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 add line to the file without delete

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+');

creating text file throws error

I am a php beginner.I want every time when the web page is open to create a file that does not exist. But every time when I run the program I have an error teling me that the file was not created.
This is my code:
$ip=$_SERVER["REMOTE_ADDR"];
if(!isset($_COOKIE['firsttime'])){
setcookie('firsttime', 'no');
$myfile = 'file/form'.$ip.'.txt';
if(file_exists($myfile) == FALSE){
$fo = fopen($myfile, 'w');
$code = '<form action = "" method = "post">';
fwrite($fo, $code);
fclose($fo);
}else{
unlink($myfile);
$file = new File();
}
}
where is my mistake?
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
Do this to open a file, and it will create the file if it doesn't exist.
Not completely sure but that would result in a pretty weird file name.
$myfile = 'file/form'.$ip.'.txt';
If my ip is 1.0.0.01.23 (really random and pretty weird) the file name would be:
file/form.1.0.0.01.23..txt
Try saving a file with that name in notepad.

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)

Categories