bachx Posted August 24, 2007 Share Posted August 24, 2007 So far I've only managed to get fwrite() to write the data at the end of a file. But I want it to update/replace an existing value instead, how can this be done? Quote Link to comment https://forums.phpfreaks.com/topic/66563-fwrite-question/ Share on other sites More sharing options...
lemmin Posted August 24, 2007 Share Posted August 24, 2007 You have to open the file usnig append, I think. I wrote this a while ago it uses fseek to move around the pointer. function insertInFile($file, $string, $loc) { $wfile = fopen($file, "r+"); if (!$wfile) return false; rewind($wfile); fseek($wfile, $loc, SEEK_SET); $endstring = fread($wfile, (filesize($file)-$loc)); fseek($wfile, $loc, SEEK_SET); fwrite($wfile, $string . $endstring); fclose($wfile); } You will have to modify it to replace existing text. Quote Link to comment https://forums.phpfreaks.com/topic/66563-fwrite-question/#findComment-333392 Share on other sites More sharing options...
bachx Posted August 25, 2007 Author Share Posted August 25, 2007 Thanks. But your function doesn't replace an existing text/value, it simply appends the $string value to wherever location that I specify. That's not really what I want. Quote Link to comment https://forums.phpfreaks.com/topic/66563-fwrite-question/#findComment-333787 Share on other sites More sharing options...
AdRock Posted August 25, 2007 Share Posted August 25, 2007 This is how I have updated a text file which replaces the whole file with what you enter <? $fund = file_get_contents($myFile); $myFile = "../includes/thermometer/testFile.txt"; $name = $_POST['fund']; // vars from form if (!isset($_POST['fund'])) { ?> <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <p style="margin-left:10px;">The current fund is:<br> <input type="text" title="Please enter the new current fund amount" name="fund" size="30" value="<? echo $fund ?>"> </form> <?} else { $fund=$_REQUEST['fund']; ?> <form method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <p style="margin-left:10px;">The current fund is:<br /> <input type="text" title="Please enter the new current fund amount" name="fund" size="30" value="<? echo $fund; ?>"> </form> <? $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = $name; fwrite($fh, $stringData); fclose($fh); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/66563-fwrite-question/#findComment-333791 Share on other sites More sharing options...
bachx Posted August 25, 2007 Author Share Posted August 25, 2007 Errrm, I don't want to replace the whole file, just a specific value. Quote Link to comment https://forums.phpfreaks.com/topic/66563-fwrite-question/#findComment-333793 Share on other sites More sharing options...
otuatail Posted August 25, 2007 Share Posted August 25, 2007 If you mean you want to overwrite the middle of the file, then you cant. Apend will append to the end of the file. Only solution is to read the entire contents make the change and write it back. Desmond. Quote Link to comment https://forums.phpfreaks.com/topic/66563-fwrite-question/#findComment-333799 Share on other sites More sharing options...
lemmin Posted August 27, 2007 Share Posted August 27, 2007 Like I said, you will have to modify my function to make it replace text instead of insert it. In order to replace the text, all you need to know is that length of the text you are replacing, then start the "$endstring" that many characters farther. Or a very simple method, as otuatail suggested, is to just read the entire file into a buffer, modify it, and overwrite the entire file with it. Quote Link to comment https://forums.phpfreaks.com/topic/66563-fwrite-question/#findComment-335307 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.