2012-04-29 129 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