Page 1 of 1

Getting the file pointer at the end of a file

Posted: Tue Jun 14, 2005 11:40 am
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!

Posted: Tue Jun 14, 2005 11:49 am
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.

Posted: Tue Jun 14, 2005 11:59 am
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!
:(

Posted: Tue Jun 14, 2005 12:43 pm
by Syranide
very strange indeed, trying hacking a little, take SET_CUR and use filesize() - 6 instead, see if that works.

Posted: Tue Jun 14, 2005 3:32 pm
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!

Posted: Wed Jun 15, 2005 5:14 am
by SabbeRubbish
Anyone?

Posted: Wed Jun 15, 2005 5:21 am
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.

Posted: Wed Jun 15, 2005 5:53 am
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..

Posted: Wed Jun 15, 2005 12:06 pm
by SabbeRubbish
Thank you very much!!! :D