Need help with File

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
tengkie
Forum Newbie
Posts: 6
Joined: Sat Apr 18, 2009 11:17 am

Need help with File

Post by tengkie »

Hi, i do really need help with file..

Let say i have a config.php:

Code: Select all

 
<?//config.php
$firstline = [XCONFIG_FIRST];
$secondline = [XCONFIG_SECOND];
$thirdline = [XCONFIG_THIRD];
?>
 
And I have other php script (this contains forms, called it replace.php) which I will use it for change all values from config.php. This script will change the [XCONFIG_FIRST]; [XCONFIG_SECOND] & [XCONFIG_THIRD] with other value that I set from replace.php.

Code: Select all

 
<!-- replace.php -->
<form method="post" action="something.php">
XConfig First : <input type="text" name="xcon_first" value=""><br/>
XConfig Second : <input type="text" name="xcon_second" value=""><br/>
XConfig Third : <input type="text" name="xcon_third" value=""><br/>
<input type="submit" value="Replace All">
</form>
 
All values submitted from replace.php will replace [XCONFIG_FIRST] and so on from config.php

Is there any ways to do it?

Thank you so much for your kind help.
MasterBeta
Forum Commoner
Posts: 38
Joined: Thu Apr 02, 2009 4:35 am
Location: Lincoln, NE

Re: Need help with File

Post by MasterBeta »

Are you trying to actually write to the file config.php and permanently replace the code?
I'm not sure I understand your question. Why not use variables or sessions, or even MySQL?
tengkie
Forum Newbie
Posts: 6
Joined: Sat Apr 18, 2009 11:17 am

Re: Need help with File

Post by tengkie »

yes, i'm trying to replace the config.php permanently.

Because in this case i'm not developing a website, but i'm trying to build a webserver using PHP. That's why i'm not using databases such as MySQL for storing server's configurations.
MasterBeta
Forum Commoner
Posts: 38
Joined: Thu Apr 02, 2009 4:35 am
Location: Lincoln, NE

Re: Need help with File

Post by MasterBeta »

Although I'm not sure this is the best way to do whatever it is you're doing here's how to do it. This will overwrite all contents with whatever it is you want. If you want to search the text and replace certain parts of the file, that's a whole different ball of wax and could get complicated with regular expressions and validation etc.

WARNING: this will overwrite ALL text inside the file (on line 2)... you've been warned!

Code: Select all

<?php
$fileName = "config.php"; // file you want to write to
$xcon_first = $_POST['xcon_first']; 
$xcon_second = $_POST['xcon_second'];
$xcon_third = $_POST['xcon_third'];
 
$error = array(); // Could be changed for form validation. At this point it only checks to ensure field wasn't left blank
foreach($_POST AS $key => $value){
    if($value == "") $error[] = "$key must not be left blank";
}
 
if(isset($_POST['xcon_first']) && count($error)<1){
$fileContents = "<?php //config.php
\$firstline = $xcon_first;
\$secondline = $xcon_second;
\$thirdline = $xcon_third;
?>";
 
$file = fopen("$fileName","w");
$writeContent = fwrite($file,"$fileContents");
fclose($file);
if($writeContent) print "$fileName updated successfully";
else print "There was an error updating file $fileName";
}else{
    foreach($error as $value) print "$value <br />";
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    XConfig First : <input type="text" name="xcon_first" value=""><br/>
    XConfig Second : <input type="text" name="xcon_second" value=""><br/>
    XConfig Third : <input type="text" name="xcon_third" value=""><br/>
    <input type="submit" value="Replace All">
    </form>
    <?php
}
?>
tengkie
Forum Newbie
Posts: 6
Joined: Sat Apr 18, 2009 11:17 am

Re: Need help with File

Post by tengkie »

i'll try this code..
thanks for your kind help...
Post Reply