| |||||||||||
| |||||||||||
open file, change content, save file Posted by hl5 (hl5), 15 September 2005 Hello:I'm trying to open a text file, change some content and then resave it. Instead of getting "newword", the file has "Resource id #3" in it. Here's the code: <?php $filename = "c:\\temp\\mydata.txt"; $mystring = fopen($filename, "wb"); $handle = fopen($filename, "wb"); $newstring = str_replace("oldword", "newword", $mystring); $numbytes = fwrite($handle, $newstring); fclose($handle); print "$numbytes bytes written\n"; ?> But in the file written, mydata.txt, I get "Resource id #3". Any ideas are much appreciated. Thanks, - Harry Posted by admin (Graham Ellis), 16 September 2005 You need to open your input file with a read attribute. That returns you a file handle through which you read the file with a function such as fread. Then make your changes to the data, re-open the file for write, and write to it.Example Code:
I've tested that code and it works. Note that opening a file returns a file handle - you must read from that if you want to access the existing data. Also note that if you're writing back to the same file that you're reading from, you much complete the read before opening the file for write! Posted by hl5 (hl5), 16 September 2005 Mr. Graham:You da man! It works well. Many thanks, - Harry Posted by curtis (curtis), 8 October 2005 An even easier way would be:Code:
Posted by hl5 (hl5), 8 October 2005 curtis:Thanks! - Harry Posted by curtis (curtis), 8 October 2005 Lol, now that I look back at Graham's, my way uses about the same amount of lines any way ![]() ![]() Just in case you might be interested, I'd recommend checking some of these out:
This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |