Training, Open Source computer languages

This is page http://www.wellho.net/forum/Writing-PHP/open-fil ... -file.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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:
$fname = "demo.txt";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));

$content = str_replace("oldword", "newword", $content);

$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);


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:
<?php
$file = 'path/to/file.txt';
$file_contents = file_get_contents($file);

$fh = fopen($file, "w");
$file_contents = str_replace('foo','bar',$file_contents);
fwrite($fh, $file_contents);
fclose($fh);
?>


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 . Anyway, I guess it doesn't hurt to know different methods to do things

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.

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho