Modify content in textfile

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mosbrickan
Forum Newbie
Posts: 2
Joined: Tue May 12, 2009 1:29 am

Modify content in textfile

Post by mosbrickan »

Let's say i have 5000 network enteties. All of them will use a similar basic config. The diffrence between them are a few rows where I need to change some IP addresses and user/pass information. And then save the new modified file in a diffrent name. This file will be placed on a TFTP server, where each network entity is going do download its unique config file.
Is it possible to modify the standrard-config-file (on the TFTP server) from an web interface in this way?
Does anyone know any good codes for this? (I'm a beginner in PHP programming )

On the webpage i need it to be simple. Just a few textboxes where you can change each row.
ex:
IP1: ........
IP2: ........
User: .......
Pass: .......
Save file as: .........


Here is an example: xxxx.cfg

I want to modify this:

Code: Select all

N ImGwaMgcpCA ImGwaMgcps.gwa.ImGwaMgcpCAs.CA1 
         A Contact 111.111.111.111 
         A Master true 
 N ImGwaMgcpCA ImGwaMgcps.gwa.ImGwaMgcpCAs.CA2 
         A Contact 222.222.222.222 
         A Master false
into this:

Code: Select all

N ImGwaMgcpCA ImGwaMgcps.gwa.ImGwaMgcpCAs.CA1 
         A Contact 333.333.333.333 
         A Master true 
 N ImGwaMgcpCA ImGwaMgcps.gwa.ImGwaMgcpCAs.CA2 
         A Contact 444.444.444.444 
         A Master false
...and then save the file to a diffrent name.
mosbrickan
Forum Newbie
Posts: 2
Joined: Tue May 12, 2009 1:29 am

Re: Modify content in textfile

Post by mosbrickan »

anyone?
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Modify content in textfile

Post by crazycoders »

Well modifying a file implies you have access to it, so first of all, does your script run in website mode or in client mode? If you run it from a CRONJOB or something similar there is a good possibility it will run as a client script.

Next, make sure your script has access to the files, you can test your write access to a folder by simply doing:

Code: Select all

 
$fp = fopen('pathtofile', 'w');
fclose($fp);
 
Finally, replacing text in a file is as simple as read the whole content as a string (As long as it fits in memory, remember most webservers limit PHP to 8MB), replace the info using STR_REPLACE or a similar mechanism then open the file again in write mode and do a fwrite of it...

Voila
Last edited by Benjamin on Wed May 13, 2009 1:10 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply