Jump to content

Altering Actual File Contents


icekat83

Recommended Posts

Hi,

 

I have a file which contains settings for a user. Similar to wordpress except simpler like setting a default email for error messages or something. The idea is that it's used as an installation script for web-ware where someone fills in a form with their database details, my script will hard-code them into the settings file and then delete itself. I'll admit I did get the idea from wordpress.

 

Only the way it's coded currently requires a certain combination of characters at the end and seems really messy. The script looks like this (and does work):

    $endpos = strpos ( $contents, '"; //replace with name//', $startpos);

 

The settings file looks like this:

    $hard_name = "Smith"; //replace with name//

 

As you can see I use the comment in the settings file, which follows what I'm actually changing, to find the end of the variable. I am sure there must be a better way of doing things but don't know what. It just seems really inefficient because if the comment is removed the script breaks.

 

Any info and tips would be much appreciated.

 

IceKat

Link to comment
https://forums.phpfreaks.com/topic/220706-altering-actual-file-contents/
Share on other sites

I've done something like that a long long long time ago, it was to administer the installation settings and maintain them. I took the path that CrayonViolent is suggesting, by storing the variables as a php script to be included, the other option is to store them as serialized data, but i wouldn't recommend saving the serialized data as just a file, a php file ok

 

<?php
$sample=array(
    'name'=>'John Doe',
     'password'=>'JaneDoe'
);

$data='<?php $config=unserialize(\''. serialize($sample) .'\'); ?>';
file_put_contents('config.php',$data);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.