用php读取/etc/ntp.conf的内容 并用html将ntp.conf的内容显示到文本框内 再实现一个按钮保存修改后的文件到原路径的完整代码
时间: 2024-02-24 21:53:30 浏览: 19
以下是用 PHP 读取 /etc/ntp.conf 文件内容并将其显示在 HTML 文本框中的代码:
```php
<?php
$filename = '/etc/ntp.conf';
$file = fopen($filename, 'r');
$content = fread($file, filesize($filename));
fclose($file);
?>
<!DOCTYPE html>
<html>
<head>
<title>读取 /etc/ntp.conf 文件内容</title>
</head>
<body>
<form action="save.php" method="post">
<textarea name="content"><?php echo $content; ?></textarea>
<br>
<input type="submit" value="保存">
</form>
</body>
</html>
```
然后,您可以使用以下代码将修改后的内容保存回 /etc/ntp.conf 文件中:
```php
<?php
$filename = '/etc/ntp.conf';
$content = $_POST['content'];
$file = fopen($filename, 'w');
fwrite($file, $content);
fclose($file);
echo '文件已保存。';
?>
```
请注意,这段代码中的 `$_POST['content']` 是从 HTML 表单中获取的文本框内容。因此,您需要在上面的 HTML 代码中添加一个表单,并将其提交到 save.php 文件中。