对于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来更改目录,但是没有管理权限,它也无法正常工作。
$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