Can one .PHP file edit another?

前端 未结 8 1895
无人共我
无人共我 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:19

    Your can write program that edit any file including php file.

    BTW. Be carefull! If you show your program how to modify itself it may do it over and over very fast, and finally start killing people like in matrix or terminator movies...

    0 讨论(0)
  • 2021-02-03 16:20

    Not talking about security, one simple way is that you can read the php file using reading functions like file_get_contents and once you have the contents, you should show them in some textarea where your users could edit it. Once they edit it and submit the form, you should update the info.

    0 讨论(0)
  • 2021-02-03 16:24

    it is possible... But you will need basic PHP skills.

    Use fopen or file_put_contents. Easiest way is to write whole PHP file from scratch...

    0 讨论(0)
  • 2021-02-03 16:27

    FILE_TO_REPLACE_IN.php:

    <?php
    define("DB_SERVER", "{DB_SERVER}");
    define("DB_USER", "{DB_USER}");
    define("DB_PASS", "{DB_PASS}");
    define("DB_NAME", "{DB_NAME}");
    

    SCRIPT_TO_CHANGE_WITH.php:

    <?php
    
    $searchF  = array('{DB_SERVER}','{DB_USER}','{DB_PASS}','{DB_NAME}');
    $replaceW = array('localhost',  'user',     'pass',     'db');
    
    $fh = fopen("FILE_TO_REPLACE_IN.php", 'w');
    $file = file_get_contents($fh);
    $file = str_replace($searchF, $replaceW, $file);
    fwrite($fh, $file);
    

    ... or something like that.

    0 讨论(0)
  • 2021-02-03 16:32

    Using fopen() fwrite() and consorts you can edit any kind of file, including php ones.

    0 讨论(0)
  • 2021-02-03 16:33

    Take a look at Zend's CodeGenerator module. Using the reflection api you can create files and classes programatically.

    0 讨论(0)
提交回复
热议问题