Editing a PHP File, with another php file, using Fwrite

后端 未结 2 519
攒了一身酷
攒了一身酷 2021-02-02 04:37

My last question wasn\'t explained very well.

What I\'m trying to do here is insert data into a PHP File, Using the fwrite feature on another .php file.

To keep

相关标签:
2条回答
  • 2021-02-02 04:44

    Try this:

    $data="echo 'hello world!';";
    $filecontent=file_get_contents('file.php');
    // position of "?>"
    $pos=strpos($filecontent, '?>');
    $filecontent=substr($filecontent, 0, $pos)."\r\n".$data."\r\n".substr($filecontent, $pos);
    file_put_contents("file.php", $filecontent);
    

    Please don't forget, that you need to check data from user.

    0 讨论(0)
  • 2021-02-02 04:58

    Ok much better alternative use a data file. Ill use json because its easy to use an very easy to parse by human eyes as well:

    // read file
    $data = file_get_contents('data.json');
    $json = json_decode($data, true);
    
    // manipulate data
    $json['users'][] = $_GET['user'];
    
    // write out file
    $dataNew = json_encode($json);
    file_put_contents('data.json', $dataNew);
    

    the reason is, php code is in the staff.php

    Well this isnt something you workaround. You should be writing/reading this kind of information form a data stor - that could be a file or a database... but not an actual script.

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