i'm working on this new system which give the option for the user to choose which system method his/her system will work on, eg: file system or database system. when installing the system they can choose the option file or database, assume that user chosed db system, later on the user can again change into databse system, is it ok to give an option like that to change when ever the userr prefers, and how can the system recognize which system its running on file or db? i thought of writing it into config file, but the that file contains other important information too.. it's not good to write in to that file that often right? should i use a nother file to write on or same config file? if config file how can i write to it? assume it has a veraible called,


and when user change it to database;


but that way have to modify file, but can't just write into it only to change that variable right?
so any ideas???
appreciate reply...

Recommended Answers

All 7 Replies

Member Avatar

Is this system for a user to use on their own site or are you hosting the thing yourself. If the latter, I can't see why the need to choose, as it's under your control, so I assume it's a system that users can download and install onto their own systems.

Using file based storage is a bit draconian, but if you insist on giving them the option, then I can't see anything wrong with using a webpage to allow them to change the contents of a different file. Your config file could be in XML, plain text (like CSV), json - any format that can be read and parsed by php. So IMO, you could read the contents of your config file to a form and then overwrite the file with new content.

You just need to ensure that any data help in the files can be transferred easily to a newly created DB. That can be fiddly.

yeah u are right, yeah i thought that so too..
so since its normal php page i don't think it can be used in this way.. thanx a lot for ur reply,

Member Avatar

What the config page is a php file?

I can't see why you can't open it, parse it and then replace a line in it. Not my choice of storage format, but should be fine.

What the config page is a php file?

I can't see why you can't open it, parse it and then replace a line in it. Not my choice of storage format, but should be fine.

yes a php file..
u mean if the code already in config file is..

$base_file = "";

define("HUKJHKJ", "kjlkjlk");
//so on

if i want to change $storage='db'; i open file.. take whole content in it as a string. look for '?>' take data before '?>' and again look for $storage='file'; and remove it, then add $storage='db'; then append '?>' at the end of the string and write the whole new content to config.php again. is that ok?
thanx for your reply..

Member Avatar

All you need to do is this:

$string = file_get_contents('config.php');
$overwrite = str_replace('$storage=\'file\';','$storage=\'db\';',$string);

THat's just a simple way - you'd probably need to use additional safeguards like file_exists() or even do a preg_replace() instead of a str_replace.
The use of single quotes is important, as double quotes will try to use the values of $storage. However you could escape the $ (\$) if you want to use double quotes - just remember to unescape the single quotes in the string.

oh thanks a lot ardav!
Really appreciate your advise and help!
Blessings and Wish you a very happy new year :)

Member Avatar

No prob. Blwyddyn Newydd Dda i ti hefyd (Happy New Year).

If solved, mark it so. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.