编写 PHP 页面实现一个留言板:留言者的需要上传id, 主题,正文和附件(附件可不传);另外设计一个页面对留言进行列表,以表格的形式展示留言的简介;当点击一条信息后,显示该留言的详细信息(把每条留言的信息都传到文件夹内)(不适应数据库,可创建文件夹存储每条留言的信息包括附件)
时间: 2024-01-22 17:17:24 浏览: 28
以下是一个简单的 PHP 留言板实现,包括留言提交和留言列表展示:
留言提交页面(message.php):
```php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>留言板</title>
</head>
<body>
<h1>留言板</h1>
<form method="post" enctype="multipart/form-data">
<label>ID:</label>
<input type="text" name="id"><br>
<label>主题:</label>
<input type="text" name="title"><br>
<label>正文:</label>
<textarea name="content"></textarea><br>
<label>附件:</label>
<input type="file" name="file"><br>
<input type="submit" name="submit" value="提交">
</form>
<?php
if (isset($_POST['submit'])) {
$id = $_POST['id'];
$title = $_POST['title'];
$content = $_POST['content'];
$filename = '';
if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) {
$filename = $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $filename);
}
$message = array(
'id' => $id,
'title' => $title,
'content' => $content,
'filename' => $filename,
'time' => time(),
);
$messages = array();
if (file_exists('messages.json')) {
$messages = json_decode(file_get_contents('messages.json'), true);
}
array_push($messages, $message);
file_put_contents('messages.json', json_encode($messages));
echo '<p>留言提交成功!</p>';
}
?>
</body>
</html>
```
留言列表页面(list.php):
```php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>留言列表</title>
</head>
<body>
<h1>留言列表</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>主题</th>
<th>时间</th>
</tr>
</thead>
<tbody>
<?php
if (file_exists('messages.json')) {
$messages = json_decode(file_get_contents('messages.json'), true);
foreach ($messages as $message) {
echo '<tr>';
echo '<td>' . $message['id'] . '</td>';
echo '<td><a href="detail.php?id=' . $message['id'] . '">' . $message['title'] . '</a></td>';
echo '<td>' . date('Y-m-d H:i:s', $message['time']) . '</td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
</body>
</html>
```
留言详情页面(detail.php):
```php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>留言详情</title>
</head>
<body>
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
if (file_exists('messages.json')) {
$messages = json_decode(file_get_contents('messages.json'), true);
foreach ($messages as $message) {
if ($message['id'] == $id) {
echo '<h1>' . $message['title'] . '</h1>';
echo '<p>' . $message['content'] . '</p>';
if ($message['filename'] != '') {
echo '<p>附件:<a href="uploads/' . $message['filename'] . '">' . $message['filename'] . '</a></p>';
}
}
}
}
}
?>
</body>
</html>
```
需要在项目目录下创建一个名为 uploads 的文件夹,用于存储上传的附件。每条留言的信息都存储在 messages.json 文件中。
注意,这个留言板的实现没有进行安全验证和过滤,仅供参考。