2012-04-29 111 views
1

我想做一个脚本,它可以自动编辑一个PHP文件。 例如,在PHP文件abc.php我有此脚本编辑PHP页面?

<?php 
/* 
Data : 'Valid Data' 
Method : 'Yes' 
*/ 
?> 

而且我想,当我通过$ _GET或另一页上$ _ POST数据获取数据到页.php,然后abc.php文件将被编辑为新的数据。同一性为替换旧值abc.php单结肠(数据:“有效数据”)

我已经试过http://www.hotscripts.com/listing/simple-php-file-editor/,但它没有提供任何方式在我的情况下使用它。

+3

除此之外,什么阻止你创建使用PHP文件'fopen' /' fwrite'? – 2012-04-29 03:39:56

+0

听起来好像你在说你想要将数组中的数据保存到PHP文档中。它是否正确? – Sampson 2012-04-29 03:39:59

+0

@Laurent说,你必须用'fopen'等文件操作。一旦读入文件,就必须像字符串一样解析它,根据需要对其进行修改,然后用'fwrite'将其写回。如果不是这样的话,那就很相似了。 – 2012-04-29 03:42:42

回答

1
//Read the text of abc.php into an array, one line per entry 
$lines = file('abc.php'); 

//Replace the third line with the new text, including your new data 
$lines[2] = "Data : '" . $some_data . "'\n"; 

//Write the new lines into abc.php 
file_put_contents('abc.php', implode($lines)); 
从所有的安全隐患
+0

Great Dan的第三行以分号分隔。它确实有帮助。 – Thompson 2012-04-29 09:52:26