Fopen Works, But Can't Fseek, Fwrite, Etc.

liunx

Guest
Maybe I'm missing something simple (usually the case). I've searched through the forums and found similar problems, but not quite the same as mine. <br /><br />I am trying to write to a file (datafile.xml) using PHP in myfile.php. <br />Here is my code:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$filetoopen=$_SERVER["DOCUMENT_ROOT"]."/travel/data/datafile.xml";<br /><br />        if ($fp = fopen($filetoopen,"r+") !== false) {<br /><br />            $newcode.='<p>Blah blah blah. Test text here.</p>';<br /><br />            fseek ($fp,-16,SEEK_END); //go to the end of the file, and back up 17 bits<br /><br />            if (fwrite($fp,$newcode,700)!==false) <br />               echo 'Success';<br />            else <br />               echo 'Error';<br />        <br />            fclose($fp);<br />        }<!--c2--></div><!--ec2--><br /><br />fopen seems to work fine. The errors I'm getting are:<br />Warning: fseek(): supplied argument is not a valid stream resource in /home/myusername/public_html/travel/myfile.php on line 43<br /><br />Warning: fwrite(): supplied argument is not a valid stream resource in /home/myusername/public_html/travel/for_us/myfile.php on line 46<br /><br />Warning: fclose(): supplied argument is not a valid stream resource in /home/myusername/public_html/travel/for_us/myfile.php on line 54<br /><br />I have even tried is_readable and is_writeable using $filetoopen and they are both true. I have tried other things such as fgets and get the same error. Permissions on datafile.xml are 666, and the directory 'data' is set to 777. I'm not sure what else to try. Thanks in advance for any help you can give. I'm so frustrated with this. <br />~LPet<!--content-->
Welcome to the forums lpet <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> <br /><br />Try;<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$filetoopen=$_SERVER["DOCUMENT_ROOT"]."/travel/data/datafile.xml";<br /><br />        $fp = fopen($filetoopen,"r+");<br />        if ($fp !== false) {<br /><br />            $newcode.='<p>Blah blah blah. Test text here.</p>';<br /><br />            fseek ($fp,-16,SEEK_END); //go to the end of the file, and back up 17 bits<br /><br />            if (fwrite($fp,$newcode,700)!==false)<br />               echo 'Success';<br />            else<br />               echo 'Error';<br />        <br />            fclose($fp);<br />        }<!--c2--></div><!--ec2--><!--content-->
Welcome to the forums lpet <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
<!--quoteo(post=206063:date=May 13 2007, 04:20 PM:name=TCH-Andy)--><div class='quotetop'>QUOTE(TCH-Andy @ May 13 2007, 04:20 PM) <a href="http://www.totalchoicehosting.com/forums/index.php?act=findpost&pid=206063"><img src='http://www.totalchoicehosting.com/forums/style_images/1/post_snapback.gif' alt='*' border='0' /></a></div><div class='quotemain'><!--quotec-->Try;<br /><br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->$filetoopen=$_SERVER["DOCUMENT_ROOT"]."/travel/data/datafile.xml";<br /><br />        $fp = fopen($filetoopen,"r+");<br />        if ($fp !== false) {<!--c2--></div><!--ec2--><!--QuoteEnd--></div><!--QuoteEEnd--><br /><br />Brilliant! Thank you so much, worked like a charm. It's simple but I don't know if I would have ever figured that one out. Learning every day. Sigh of relief.<!--content-->
Welcome to the forum, lpet. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
Welcome to the forums, lpet <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /><!--content-->
 
Back
Top