Jump to content

Recommended Posts

Posted

I have a config file that stores some Global constants

The file sending the info has a standard form that reads the constants

and submits them to the page listed below.

 

The config file:

$global_this = "thisconstant"

$global_that = "thatconstant"

 

I just need to change the constant part not the whole thing???

Any help...This is what I have and its working...

Except it appends the whole string to the end of the file...

Thanks Guys

 

<?php

$sitename = $_POST['sitename'];

echo "<html><head><title>$sitename</title></head><body>";
echo "$sitename will be added to record on ";
echo $date;
echo "<br><br>";
$outputstring = "\$global_sitename=$sitename\n";
@ $fp = fopen("config.php", "a");
flock($fp, 2); 
if (!$fp)
{
    echo "<p><strong> Your Addition could not be processed at this time.
         Please try again in a few minutes.</strong></p></body></html>";
    exit;
} 
fwrite($fp, $outputstring);
flock($fp, 3); 
fclose($fp);
echo "<center><p><b><a href=\"test.php\">Click Here to continue</a></b></p></center>"; 


?>

Posted

I'm not real clear on what your problem is and my suggestion may be way off, but if your talking about what your writing to your config.php in this line:

 

@ $fp = fopen("config.php", "a");

 

then "a" is why your appending. See if this helps you out?

 

http://byrondallas.heliohost.org/php/101/file_writing.txt

 

EDIT:

 

btw, here's a little better way of writing to a file:

 

http://us3.php.net/manual/en/function.file-put-contents.php

 

Posted

Ok maybe I didn't get it right the first time...lol

I know that I will have to change the write attribute "a"

 

This is what I'm after

 

I have a line that reads:

$global_sitename="The James Boys";

and I just want to change the red part

$global_sitename="The James Boys";

there are dozens of globals that I have placed in this file, now I'm writing the

Admin section of the site and need a way to configure all aspects of the site

not just the easy part...as in database... silly file stuff I know but I never had the need before now...

 

Thanks Byron

 

I'm not real clear on what your problem is and my suggestion may be way off, but if your talking about what your writing to your config.php in this line:

 

@ $fp = fopen("config.php", "a");

 

then "a" is why your appending. See if this helps you out?

 

http://byrondallas.heliohost.org/php/101/file_writing.txt

 

EDIT:

 

btw, here's a little better way of writing to a file:

 

http://us3.php.net/manual/en/function.file-put-contents.php

 

Posted

How about preg_replace?

 

$sitename = preg_replace("@The James Boys@", "New Sitename", $sitename);

 

 

 

 

 

Posted

See if this helps you any. This will open a file and get it's contents, then search for 'whatever' and replace it with 'whatever' and put the contents back.

 

$file = "config.php";

# open the file to get existing content
$current = file_get_contents($file);

# search and replace
$fp = preg_replace("@The James Boys@", "$new_site_name", $current);

# write the contents back to the file
file_put_contents($file, $fp);

 

or your way (fopen, fread, fwrite):

 

# open file for reading and changing
$filename = "config.php";
$file = fopen($filename, "r");
$contents = fread($file, filesize($filename));
$newcontents = preg_replace("@The James Boys@", "$new_site_name", $contents);
fclose($file);

# rewrite new file with changes
$file = fopen($filename, "w");
fwrite($file, $newcontents);
fclose($file);

 

 

 

 

Posted

Byron I appreciate all the help...

I've googled till I can't google anymore...

Ok this is getting closer, I'm working with the first 7 variables, I think an array would be the way to go.

 

If I can get them into an array then I can just use the array to output it to the file.

 

//get the old variables loaded first
require_once('../config.php');

$global_sitename = $_POST['new_sitename'];
$username = $_POST['new_username'];
$password = $_POST['new_password'];
$database = $_POST['new_database'];
$myserver = $_POST['new_server'];
$config_base_url = $_POST['new_base_url'];
$config_index_page = $_POST['new_index_page'];

 

================

I tried this and all it did was output the word array to the screen:

$newstuff = array($global_sitename=>$_POST['new_sitename'], $username=>$_POST['new_username'], $password=>$_POST['new_password']); 

echo $newstuff . "<br />";

 

 

I think Im going about it all wrong...you would think that this would be widely used...

Joomla uses the same thing with it's framework...

 

I found a file that I think I can use that has everything in it that I need.

Take a look at it and see what you think...Thanks

Posted

Yes it has been slow for me for days now.

I finally got it working just minutes ago... phew...

This is not much different than calling a function in VB6...

That's where I spend most of my time.

Thanks for all the help. If anyone needs this code it's free for the taking.

Glad I could be of help.

 

Wrote a function to do whats needed...

<?php
//Include database connection details
require_once('../config.php');
// php file example : write string variable to text file




    function replaceinFile($fFile, $sStr, $sRep) {
        $fArray = file($fFile); // Put the file into an array
        $fNew = array(); // Create a new array so we can output the results
        $fFile = fopen($fFile, "w"); // Open the file
        
        // Loop through each array index and output the value
        foreach ($fArray as $fArrayNum => $fArr) {
            $fNew[] = str_replace($sStr, $sRep, $fArr); // Replace anything in the array which matches our variables
        }
        
        // Loop through each new array index and output the value
        foreach ($fNew as $fNewNum => $nArr) {
            fwrite($fFile, $fNew[$fNewNum]); // Write the new data from the array
        }
        
        fclose($fFile); // Close the file
    }





echo $_POST["new_sitename"];
echo '<br />';
echo $global_sitename;

replaceinFile('../config.php',$global_sitename,$_POST["new_sitename"])

//replaceinFile('../config.php',"The James Boys","The James Boys2")

?>

Posted

Glad you finally came up with something. :) Sorry I wasn't more of help, but I never was really sure exactly what you were trying to do so I was just suggesting simple fixes for what I did understand. :)

 

Cheers,

Byron

 

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...