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+');
I am trying to open multiple files then split each file by line. I am using this with one foreach() to browse through files and another foreach() inside of it to split the file's lines.
$dir = '../mydir';
$files = scandir($dir, 1);
foreach($files as $input){
if(($input!==".")&&($input!=="..")){
$myfile="../mydir/".$input;
$fh = fopen($myfile, 'r');
echo $input."... ";
$input = substr($input,0,-4);
$FN = "../mydir/out.".$input.".xls";
$FH = fopen($FN, 'w') or die("Can't open file!");
$lines = file($myFile);
foreach($lines as $line) {
list($OPT1,$OPT2) = explode(",", $line);
fwrite($FH, $OPT1);
}
echo "Done.";
}
}
But my $OPT1 is empty.
I suspect your $fh = fopen($myfile,'r') is locking the file so file() can't access it. You don't appear to be using that file handle (indeed you are overwriting the variable later) so just remove that line and you should be good.
EDIT: Oh, and apparently variable names are case-sensetive. You can't interchange $myfile and $myFile.
I have a file called 1.txt and I am trying to display its part on the website.
I am trying to display a specific part between two keywords.
The file can be visible here: http://pastebin.com/GwVKhs8h
I want to display the text between these words: "(Non-Privileged).pst" and "-----Original Message-----" so it would return:
Please let me know if you still need Curve Shift.
I am using the following code:
<?php
$myFile = "1.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
if (preg_match("/\(Non-Privileged\)\.pst((\n|.)*)-----Original Message-----/", $theData, $matches1))
{
echo $matches1[1]."<br />";
}
?>
and I tried few things but it just doesn't work.
Use explode.
$myFile = "1.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$pairs = explode("(Non-Privileged).pst", $theData);
$pairs = explode("-----Original Message-----", $pairs[1]);
$text = $pairs[0];
print $text;
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)
<?php
function saveFile($url, $filename) {
$data = file_get_contents($url);
$fh = fopen($filename, 'w') or die("can't open file");
fwrite($fh, $data);
fclose($fh);
}
$myFile = "images.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
$data = explode("\n", $theData);
$count = count($data);
for($i=0;$i<=$count;$i++) {
saveFile($data[$i], basename($data[$i]));
}
?>
This is my code and every time i replace a dynamic file name it prints error
Warning: fopen(10017226314.jpg ) [function.fopen]: failed to open stream: Invalid argument in C:\xampp\htdocs\download\download.php on line 4
can't open file
What should be the cause? please help. Thanks
Please replace your fopen function parameter from w to r+
function saveFile($url, $filename) {
$data = file_get_contents($url);
$fh = **fopen($filename, 'r+')** or die("can't open file");
fwrite($fh, $data);
fclose($fh);
}