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

做~自己de王妃 提交于 2019-12-02 19:50:39

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!