如何使用Windows的PHP脚本动态更新我的driver / etc / hosts文件?

问题描述 投票:1回答:1

对于Windows,我正在尝试使用下面的PHP代码动态地编辑和更新我的'drivers / etc / hosts'。通过此代码,我可以轻松地更新不需要管理权限的任何文件。但是在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");


 ?>

我正在使用“ chdir来更改目录,但是没有管理权限,它也无法正常工作。

php windows host hosts
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.