I am looking to open up a file, grab the last line in the file where the line = "?>", which is the closing tag for a php document. Than I am wanting to append data into it and add back in the "?>" to the very last line.
I've been trying a few approaches, but I'm not having any luck.
Here's what I got so far, as I am reading from a zip file. Though I know this is all wrong, just needing some help with this please...
// Open for reading is all we can do with zips and is all we need.
if (zip_entry_open($zipOpen, $zipFile, "r"))
{
$fstream = zip_entry_read($zipFile, zip_entry_filesize($zipFile));
// Strip out any php tags from here. A Bit weak, but we can improve this later.
$fstream = str_replace(array('?>', '<?php', '<?'), '', $fstream) . '?>';
$fp = fopen($curr_lang_file, 'r+');
while (!feof($fp))
{
$output = fgets($fp, 16384);
if (trim($output) == '?>')
break;
}
fclose($fp);
file_put_contents($curr_lang_file, $fstream, FILE_APPEND);
}
$curr_lang_file
is a filepath string to the actual file that needs to have the fstream appended to it, but after we remove the last line that equals '?>'
Ok, I actually made a few changes, and it seems to work, BUT it now copies the data in there twice... arggg, so each line in the file is now in there 2 times :(
Ok, removed the fwrite, though now it is appending it at the bottom, just below the ?>
OMG, I just need everything up to the last line, isn't there a way to do this??? I don't need "?>"