如何使用Windows脚本动态更新我的驱动程序/ etc / hosts文件?

时间:2018-05-15 06:42:20

标签: php windows host hosts

对于Windows,我正在尝试动态编辑和更新我的' drivers / etc / hosts'使用下面的PHP代码。通过此代码,我可以轻松更新任何不需要管理权限的文件。但在Windows中,我需要管理权限才能从此(' Windows / System32 / drivers / etc /')目录中打开/更新主机文件。我应该使用哪种语法在PHP脚本中打开具有管理权限的主机文件?

<?php

 echo "Hi this Host Testing";

 $currentdir=getcwd();  // it will save your current directory location    

 chdir('../../../../Windows/System32/drivers/etc/');  // it will change my 
 web to etc directory (/etc/)

 $file='hosts';
 $current=file_get_contents($file);
 $string_Data = 'Any Data';

 $current.=$string_Data."\r\n";;
 if(file_put_contents($file, $current))
 {
   echo "success in writing";
 }
 else
 {
   echo "fail in writing";
 }

 chdir("$currentdir");


 ?>

我正在使用&#34; chdir &#34;要更改目录但没有管理权限,它也不起作用。

1 个答案:

答案 0 :(得分:0)

$currentdir=getcwd();  // it will save your current directory location    

chdir('../../../../etc/');  // it will change your web directory( /var/www/html/website/)  to etc directory (/etc/)

$file='hosts';
$current=file_get_contents($file);
$string_Data = 'It works success!!';
$current.=$string_Data;
if(file_put_contents($file, $current))
{
echo "success in writing";
}
else
{
echo "fail in writing";
}

chdir("$currentdir"); // it will change directory (/etc/) to your web directory( /var/www/html/website/ )

//change permission before run php page because if your user don't have permission this code can't do writing