我嘗試使用下面的PHP代碼如何動態編輯和使用PHP腳本
$string_Data = 'It works success!!';
$file_To_Edit = "/etc/hosts";
exec("sudo chmod 777 /etc/hosts");
$opened_File = file($file_To_Edit) or die("Error. Code:2 - Can not open file $file_To_Edit");
$lines = preg_grep(' /test.com/', $opened_File);
foreach(array_keys($lines) as $key) {
$opened_File[$key] = substr($opened_File[$key], 0, -1) . $string_Data;
}
$f = fopen($file_To_Edit, 'w') or die("Error $file_To_Edit");
fwrite($f, implode("\n", $opened_File));
fclose($f);
誰能告訴我我在哪裏動態編輯和更新我的/ etc/hosts文件更新我的etc/hosts文件出錯了,因爲我無法更新/ etc/hosts文件。
我認爲你需要運行該腳本根和類似的exec( 「sudo的搭配chmod 777/etc/hosts文件」)根的權利;是安全問題恕我直言 – drupality