4WebHelp
 FAQ  •  Search  •  User Groups  •  Forum Admins  •  Smilies List  •  Statistics  •  Rules   •  Login   •  Register
Toggle Navigation Menu

 PHP fwrite/fopen overwrites characters when writing
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic
Author Message
ScottB
Junior WebHelper
Junior WebHelper


Joined: 04 Jul 2005
Posts: 4
Location: Britain

PostPosted: Mon Jul 04, 2005 1:23 pm (12 years, 5 months ago) Reply with QuoteBack to Top

Im using a combination of the fopen and fwrite tags to add an entry to an rss file (with the .txt extension for testing). I opened the file named 'writing.txt' in r+ mode, and use fseek to change the position of the file pointer. I can write to the file at the point I want it too easy, but it overwrites the characters after the file pointer until it has finished the string. Is there any way of shifting the text after a certain point in a string along a certain amount of characters or setting fopen to an alternative mode to fix this problem?

writing.txt
Code:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">

<channel>
    <title>Main Title</title>
    <description>This is the main title description</description>
    <link>http://www.google.co.uk</link>

<item>
    <title>Second Title</title>
    <description>This is the second title description</description>
    <link>http://www.google.co.uk</link>
</item>

<item>
    <title>Third Title</title>
    <description>This is the third title description</description>
    <link>http://www.google.co.uk</link>
</item>

</channel>
</rss>


php file
Code:

<?php
$add = "<item>
    <title>First Title</title>
    <description>This is the first title description</description>
    <link>http://www.google.co.uk</link>
</item>";

$xml = file_get_contents("writing.txt");
$handler = fopen("writing.txt", "r+");
fseek($handler, strpos($xml, "<item>"));
fwrite($handler, $add);
fclose($handler);
?>


When the php file is viewed, writing.txt ends up as this:
Code:

<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">

<channel>
    <title>Main Title</title>
    <description>This is the main title description</description>
    <link>http://www.google.co.uk</link>

<item>
    <title>First Title</title>
    <description>This is the first title description</description>
    <link>http://www.google.co.uk</link>
</item>m>

<item>
    <title>Third Title</title>
    <description>This is the third title description</description>
    <link>http://www.google.co.uk</link>
</item>

</channel>
</rss>


Notice the second item has been overwritten leaving only the partial </item> tag.

Thanks.
OfflineView User's ProfileFind all posts by ScottBSend Personal MessageVisit Poster's Website
Display posts from previous:      
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic


 Jump to:   




You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot edit your posts in this forum.
You cannot delete your posts in this forum.
You cannot vote in polls in this forum.


Page generation time: 0.048683 seconds :: 17 queries executed :: All Times are GMT
Powered by phpBB 2.0 © 2001, 2002 phpBB Group :: Based on an FI Theme