How to open a file and remove the last line?

Posted by sologhost on Stack Overflow See other posts from Stack Overflow or by sologhost
Published on 2010-04-26T05:17:01Z Indexed on 2010/04/26 5:33 UTC
Read the original article Hit count: 154

Filed under:
|
|
|

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));
    $fp = fopen($curr_lang_file, 'r+b');

    while (!feof($fp))
    {
        $output = fgets($fp, 16384);
        if (trim($output) == '?>')
            break;

        fwrite($fp, $output);
    }

    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 '?>'

© Stack Overflow or respective owner

Related posts about php

Related posts about fgets