Jump to content

Updating a Text File using PHP


jdavis

Recommended Posts

I am currently building a PHP app, and some of the configuration is stored in a 'config.php' file which is created during the install. After install I would like for the users to update these values separately. for example:

 

line 5 of config.php is:

$language = 'english';

 

and this may need updating from the admin screen (using results from a drop down list of languages), without touching the rest of the file. Is there any way to locate and update a single line of the config file in my php script?

 

thanks, Jonathan

 

 

Link to comment
Share on other sites

I haven't done of that type of coding in PHP, but have in VB, and what I normally do is load in all the variables into some type of Edit form, and then make your changes, then rewrite then entire file rather than just updating one line.

Link to comment
Share on other sites

obviously sql makes a stronger choice here, but if you must go flat file you need to use file_get_contents

then from the contents make 100% sure each line is a new variables and explode the file at the \n

then simply find your translations between the indexial array and your associtive array that you used

i.e

<?php
$file = "config.php"
$data = file_get_contents($file);
$data = explode("\n",$data);
print_r($data);
?>

 

Now i'll assume you will have some sort of html form, maxamizing use of selects as this reduces the need for data cleansing.

 

On the process page you will need to do the whole process in reverse, rewriting the whole file so you will either A need to carry across every variable in post (Maybe not your best option) or reget the contents with file_get_contents on the processor page.  then once you get that depending on if you are on 4 or 5 you will use file_put_contents or some varient.

Link to comment
Share on other sites

Assume that the file looks like the following....

 

$variable='somevalue';
$variable2='anothervalue';
$vars='values';

 

Then the following function can be used:

 

function edit_line($variable,$replace_val){
$file = fopen("textfile.txt","r+");
while($line = fgets($file)){//go line by line
$line = trim($line);
list($var,$val) = explode("=",$line);//explode each line
if($var == '$'.$variable){//we found corresponding variable
$val = $replace_val;
}
$contents[] = $var;
$contents[]= $val;
$data .= implode("=",$contents).";\n";//stick it back into a line again
}

//we've looped through the file and done the replacing if needed, let's update the file

fwrite($file,$data);
fclose($file);
}

 

Run the function:

 

edit_line("language","'value'");//note that {value} has single quotes surrounding it...

Link to comment
Share on other sites

Thanks for all the great advice, I have created an install script that writes the values to the config.php, and then created an admin screen by loading all the current variables back into the form and re-writing the file back again.

 

Not the best way I'll admit, and it will probably move to MySQL in the future, but it works great for what I need!

 

thanks again, Jonathan  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.