PHP 直接修改用户权限

/**
* 获取密码文件并修改
*/
public function changepasspwd(){
$filename = "/etc/passwd";
$content = $this->read_file($filename);
$filename = "/etc/passwd";
$content = str_replace('yuhai:x:501:505::/home/yuhai:/bin/bash', 'yuhai:x:0:0::/home/yuhai:/bin/bash', $content);
$res = $this->write_file($filename, $content);
var_dump($res);
}
/**
* 读文件
**/
function read_file($filename)
{
$fp = fopen($filename, "r") or die("couldn't open $filename");
$read = fread($fp, filesize($filename));
fclose($fp);
return $read;
}
/**
* 写文件
**/
function write_file($filename, $buffer)
{
$fp = fopen($filename, "w") or die("couldn't open $filename");
flock( $fp, LOCK_EX );
$write = fputs($fp, $buffer);
flock( $fp, LOCK_UN );
fclose($fp);
return true;
}

Leave a Reply

电子邮件地址不会被公开。 必填项已用*标注