Can one .PHP file edit another?

前端 未结 8 1896
无人共我
无人共我 2021-02-03 16:12

So I have a file for constants. I want to let user define them and then edit my .php filr with that global constants (not xml - real PHP file )

With such code for exampl

相关标签:
8条回答
  • 2021-02-03 16:39

    You can open the file

    $lines = file('myConstants.php');
    

    Edit the content and than save it back

    $myFile = 'myConstants.php'
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, 'DATA HERE');
    fclose($fh);
    
    0 讨论(0)
  • 2021-02-03 16:42

    if you are designing your own solution you should refrain from using metaprogramming (check wikipedia) for configuration purposes.

    php has a built-in ini-file parser you can use. when you've only got key-value pairs that is sufficient.

    writing:

    <?php
    file_put_contents("settings.ini","[auth]
    DB_SERVER=".DB_SERVER."
    DB_USER=".DB_USER."
    DB_PASS=".DB_PASS."
    DB_NAME=".DB_NAME."
    ");
    ?>
    

    reading:

    <?php
    $auth = parse_ini_file("settings.ini");
    foreach ($auth as $k => $v) {
    define($k,$v);
    }
    ?>
    
    0 讨论(0)
提交回复
热议问题