Page 1 of 1

Modify content in textfile

Posted: Tue May 12, 2009 1:46 am
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.

Re: Modify content in textfile

Posted: Wed May 13, 2009 10:51 am
by mosbrickan
anyone?

Re: Modify content in textfile

Posted: Wed May 13, 2009 11:36 am
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