Forum Moderators: coopster

Message Too Old, No Replies

file handling

         

johnnybgoode

9:36 pm on Dec 2, 2006 (gmt 0)

10+ Year Member



I want to be able to read/write to a config file for a php script from a control panel. I want to know all the different ways I can do it, and the pros and cons of each.

This is how the Control Panel will be:
---------------------------------------------------------------------
controlpanel.php

MySQL Settings:

DB Server: (single line textbox here)
DB Name: (single line textbox here)
DB Username: (single line textbox here)
DB Password: (single line textbox here)

(save config button here)

---------------------------------------------------------------------
config.php

/* --------MySQL Settings-------- */

define("DB_SERVER", "localhost");

define("DB_USER", "username");

define("DB_PASS", "password");

define("DB_NAME", "whatever");

Ok... so I guess what I need to know is can I read ONLY one part of a file like... localhost? And then display it in the control panel input box by DB Server. Then, make only that portion of the config file allowed to be edited. or what? how should I do this?

justageek

12:23 am on Dec 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could do it easy enough by just reading in the file one line at a time and looking for the lines that have the static text as you have it in your post. Store those lines in an array and display however you wish. When you save to the file just read the file again and look for those lines that have the text then replace and save. File() will read the lines into an array so you can pop out the lines you want.

I'm not sure what you want to use it for but keep in mind that the settings you show in your example could lead to trouble if you don't do some authentication as to who can change what and when. You alos need to look out for sql injections that would cause harm.

JAG

johnnybgoode

1:33 am on Dec 3, 2006 (gmt 0)

10+ Year Member



justageek,

first of all, thanks for a quick response...

NOTE: The control panel has a login, and stripslashes are in effect in the script.

I see where you are going and I understand the logic in doing it that way but I just don't know how to code it. Would you be kind enough to code an example of some sort?

Thanks,
Johnny

johnnybgoode

4:02 am on Dec 3, 2006 (gmt 0)

10+ Year Member



nevermind... I figured it out. Thanks so much for your help.

Johnny