views:

99

answers:

3

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

A: 

It's copying each line twice because of your line that says:

 @fwrite($fp, $output);

You're just reading the file to find the end tag, there's no need to write the line you've read as you read.

ghoppe
Ok, thanks, but I need to get rid of the last line where the line = "?>" unless there is a better way to do this...?
SoLoGHoST
Well, I took that line out, and now it's not removing "?>" at all :(
SoLoGHoST
A: 

I would add a new <? YOUR CODE ?> after the last line...

Or if you have the new PHP code to add in $code this could be done with a regular expression

$output = preg_replace('\?>','?>'.$code,$output)

Osama ALASSIRY
I'm confused, what does this do exactly? Can you show me a better example perhaps, using the code I have above? It's just that $fstream can have php tags in it also, which I believe they should be stripped out too??
SoLoGHoST
The `$fstream` = the code I need placed within the php file either after `"<?php"` or before `"?>"`, doesn't matter I suppose. But I'd prefer it be just before `"?>"`. Am I closing the file too soon, should I be writing to it differently? OMG, I'm so lost here, I feel brain-dead!
SoLoGHoST
Ok, thanks, I am using `"\n" . '<?' . MY CODE . '?>'`, however, just wondering can I use `<?php` instead of `<?`, or can it not be in there twice in a php document? I feel stupid here as I'm still learning everything...
SoLoGHoST
I can't understand how preg_replace works, even after reading the php manual on it. Just doesn't make any sense to me.
SoLoGHoST
You're also assuming just one php block in the file...
Osama ALASSIRY
@sologhost: You can have as many <?php ?> blocks as you want in a file. There's no practical limit, other than how ugly and unreadable the code can get if you sprinkle a few million or billion of them around
Marc B
@osama: This will append the code to EVERY ?> tag within the file. That's ok in this case, if sologhost only has the one closing tag, but otherwise you're going to end up with a huge spaghetti file.
Marc B
+2  A: 

A simple way with the file on the filesystem:

$path = "/path/to/file";
$content = file($path); // Parse file into an array by newline

array_pop($content);

file_put_contents(implode($content));
Cez
The lines in the array returned by `file` do still contain the line break character sequence!
Gumbo
@Gumbo Yes, good point. implode() call has been edited
Cez