代码之家  ›  专栏  ›  技术社区  ›  Alec Smart

使用PHP编辑PHP(管理中心)

php
  •  2
  • Alec Smart  · 技术社区  · 15 年前

       <?php
         $option1 = 1;
         $option2 = 2;
         $option4 = 5;
         $option7 = array('test','a','b',c');
       ?>
    

    现在说,在一个管理页面,我只会改变一些值,如option2或option4等,有什么想法是最好的方式去做这件事。

    3 回复  |  直到 15 年前
        1
  •  2
  •   Benjamin Delichere    15 年前

    您所要做的就是用 parse_ini_file ,然后修改该数组中的值,最后用新值覆盖文件,如中所述 this comment .

    出于明显的安全原因,最好将这些文件放在文档根目录之外。

    [first_section]
    one = 1
    five = 5
    animal = BIRD
    
    [second_section]
    path = "/usr/local/bin"
    URL = "http://www.example.com/~username"
    

    safefilewrite 功能):

    <?php
    $ini_file = '/path/to/file.ini';
    $ini_array = parse_ini_file($ini_file);
    
    $ini_array['animal'] = 'CAT';
    
    safefilerewrite($file, implode("\r\n", $ini_array));
    ?>
    
        2
  •  1
  •   Your Common Sense    15 年前

    var_export()可能就是您要查找的函数。

        3
  •  0
  •   Robijntje007    15 年前

    可以使用以下代码将设置写入/读取到文件:

    $content = array();
    //fill your array with settings;
    $fh = fopen ( $bashfile, 'w' ) or die ( "can't open file" );
    fwrite ( $fh, $content );
    fclose ( $fh );
    

    要阅读它,请使用: file \u get \u contents()//这将返回一个字符串值 或者

    $lines = file('file.txt');
    //loop through our array, show HTML source as HTML source; and line numbers too.
    foreach ($lines as $line_num => $line) {
    print "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
    }
    
    推荐文章