Contact Us Forum Rules Search Archive
WebProWorld Part of WebProNews.com
Page One Link To Us Edit Profile Private Messages Archives FAQ RSS Feeds  
 

Go Back   WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Subscribe to the Newsletter FREE!


Register FAQ Members List Calendar Arcade Chatbox Mark Forums Read

Web Programming Discussion Forum Working with an API? Developing a plugin? Writing a Mod or script for your favorite blog, Web 2.0 site or Forum? Welcome.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-11-2004, 10:26 AM
WebProWorld New Member
 

Join Date: Mar 2004
Location: EarthPlanet
Posts: 2
Nasimov RepRank 0
Default Writing to a file. (PHP)

It's possible to ammend data within a file vs. just adding data to the beginning or the end of the file? Mean to change something in the "middle" of the file.
Thanks.
Reply With Quote
  #2 (permalink)  
Old 03-12-2004, 12:18 PM
WebProWorld New Member
 

Join Date: Dec 2003
Location: WA, USA
Posts: 18
bob_99 RepRank 0
Default Re: Writing to a file. (PHP)

How about:

Read all of the file into a variable

Do a search to insert the new data into the middle of the variable

Write variable back to file


I hope this helps,

Bob

Quote:
Originally Posted by Nasimov
It's possible to ammend data within a file vs. just adding data to the beginning or the end of the file? Mean to change something in the "middle" of the file.
Thanks.
Reply With Quote
  #3 (permalink)  
Old 03-21-2004, 06:37 PM
WebProWorld Member
 

Join Date: Jul 2003
Location: Toronto, Canada
Posts: 83
jamal RepRank 0
Default

Here:
Code:
<?php
$fileContent = implode("", file("filename"));
//Replace a string
$fileContent = str_replace($old, $new, $fileContent);
//OR you can insert/delete a string at a specific point
//in the variable, do whatever processing you need.
$fp = fopen("filename", "w");
fputs($fp, $fileContent);
fclose($fp);
?>
If you want to add something to the end of a file:
Code:
<?php
$fp = fopen("filename", "a");
fputs($fp, "Whatever text");
fclose($fp);
?>
At the start of a file:
Code:
<?php
$fileContent = implode("", file("filename"));
$fileContent = "New Text" . $fileContent;
//Then just write to file using "w"
?>

HTH,
Jamal
Reply With Quote
Reply

  WebProWorld > Webmaster, IT and Security Discussion > Web Programming Discussion Forum
Tags: , ,



Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Search Engine Optimization by vBSEO 3.2.0