Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
打开php文件,更改一个变量的值,保存_Php - Fatal编程技术网

打开php文件,更改一个变量的值,保存

打开php文件,更改一个变量的值,保存,php,Php,我试图修改变量$denumire produs=''的值;从一个php文件_inc.config.php到一个脚本,通过这个代码和index.php文件中的表单,我有一些错误。 变量的新值将成为通过表单从键盘输入的值。 有人能帮我吗 <?php if (isset($_POST['modify'])) { $str_continut_post = $_POST['modify']; if (strlen($_POST['search']) > 0 || 1==1

我试图修改变量$denumire produs=''的值;从一个php文件_inc.config.php到一个脚本,通过这个代码和index.php文件中的表单,我有一些错误。 变量的新值将成为通过表单从键盘输入的值。 有人能帮我吗

<?php 

if (isset($_POST['modify'])) {
    $str_continut_post = $_POST['modify'];

    if (strlen($_POST['search']) > 0 || 1==1) {
        $fisier = "ask003/inc/_inc.config.php";
        $fisier = fopen($fisier,"w") or die("Unable to open file!");

        while(! feof($fisier)) {
            $contents = file_get_contents($fisier);
            $contents = str_replace("$denumire_produs =' ';", "$denumire_produs ='$str_continut_post';", $contents);
            file_put_contents($fisier, $contents);

            echo $contents;
        }
        fclose($fisier);
        die("tests");
    }
}
?>

 <form  method="POST" action="index.php"  >  
    <label>Modifica denumire baza de date: </label>  
    <input  type="text" name="den"> 
    <button type="submit" name="modify"> <center>Modifica</center></button>

     </div></div>
  </form> 
这是一个XY问题

与其让某种系统开始重写自己的文件,为什么不让带有要更改的变量的文件加载json配置文件

{
    "name": "Bob",
    "job": "Tea Boy"
}
然后在脚本中:

$json = file_get_contents('/path/to/config.json');
$config = json_decode($json, true);
$name = $config['name'];
更改配置中的值非常简单,只需编码一个数组并将json放入文件中

$config['denumireProdu'] = 'something';
$json = json_encode($config);
file_put_contents('/path/to/config.json', $json);
这比让PHP重写自己要明智得多

这些命令的文档:


这看起来很像一个XY问题。为什么你的代码会自动编辑你的代码?如果你使用file\u get\u contents,你就得到了文件的全部内容,这样你就不需要在出现什么错误之前进行fopen了?为什么不直接覆盖变量呢?一场使用代码编辑代码有很多更好的方法。看起来您希望用户能够设置一些配置。将这些配置存储在数据库中不是更容易吗?var_导出在这种情况下也能很好地工作,创建一个包含实际可解析PHP代码的文件…可以省去手动解码JSON的麻烦,您仍然可以在任何地方包含这样的配置文件,而不需要额外的数据解析步骤。有趣的是,为什么不写一个例子,并坚持在一个答案?基本上,答案已经存在-