Getting the file pointer at the end of a file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SabbeRubbish
Forum Newbie
Posts: 10
Joined: Fri May 20, 2005 7:15 am
Location: Belgium

Getting the file pointer at the end of a file

Post by SabbeRubbish »

Hi all, again for a tremendously stupid question probably :P but I've searched the web for a while already, not finding what I need... Thus I come to you for aid :wink:

My problem: I was already successful in an attempt to parse an XML file (just parsing, no DTD). Now I want to add some information to this XML file. I tried to do this using the standard file tools: fopen, fseek, fwrite.

I wanted to edit the following file:

Code: Select all

<?xml version=&quote;1.0&quote; encoding=&quote;ISO-8859-1&quote;?>
<news>
  <topic>
    <title>XML Parser works!</title>
    <date>14-06-2005</date>
    <body>Body text</body>
  </topic>
</news>
To add a new entry, a new "topic" to be exact, I would want to remove the last line

Code: Select all

</news>
and replace it by some new tags.

So i did the following:

Code: Select all

$news_xml = fopen(&quote;news.xml&quote;,&quote;w+&quote;);
echo fseek($news_xml,-6,SEEK_END);
Output gives me:

Code: Select all

-1
Meaning the seek was NOT done...
I have tried nearly anything to make this work...
Also note that seek won't work when in Append mode ("a" and "a+"), as stated on php.net (http://uk2.php.net/fseek):
Note: If you have opened the file in append ("a" or "a+") mode, any data you write to the file will always be appended, regardless of the file position.
Any ideas? Or is there some kind of "smart" way of adding data to an XML file?
Thanks in advance!
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

are you sure you are really getting a filepointer?
otherwise I recommend you to use some XML-functionality, as most allows for reading, modifiying, and saving.

but I'm guessing on your filepointer.
SabbeRubbish
Forum Newbie
Posts: 10
Joined: Fri May 20, 2005 7:15 am
Location: Belgium

Post by SabbeRubbish »

Syranide wrote:but I'm guessing on your filepointer.

Code: Select all

$news_xml = fopen("news.xml","a+");
print_r(fread($news_xml,10));
Outputs:

Code: Select all

<?xml version=&quote;1.0&quote;
So I don't think the filepointer is wrong, the file is there and can be read!
:(
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

very strange indeed, trying hacking a little, take SET_CUR and use filesize() - 6 instead, see if that works.
SabbeRubbish
Forum Newbie
Posts: 10
Joined: Fri May 20, 2005 7:15 am
Location: Belgium

Post by SabbeRubbish »

Syranide wrote:very strange indeed, trying hacking a little, take SET_CUR and use filesize() - 6 instead, see if that works.
Indeed! This works now! Why the "-6" didn't work, don't ask, but all seems te work fine now.

Although, this manually editing of my XML file is not really pleasing me... Could you tell me a bit more about how to edit and save changes to xml files?

Thanks ahead!
SabbeRubbish
Forum Newbie
Posts: 10
Joined: Fri May 20, 2005 7:15 am
Location: Belgium

Post by SabbeRubbish »

Anyone?
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

trying taking a search on XML in the manual and you will find a few different, don't know if xml_ will do it, however, most of them are not installed, or which ones depends on what server and so on.

there are PHP-projects which does this too, however do have in mind the great performance hit by using them.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

http://www.php.net/xml
http://www.php.net/domxml

These libraries have functions that allow you to dump the xml into memory and file..
SabbeRubbish
Forum Newbie
Posts: 10
Joined: Fri May 20, 2005 7:15 am
Location: Belgium

Post by SabbeRubbish »

Thank you very much!!! :D
Post Reply