-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.php
170 lines (161 loc) · 5.4 KB
/
upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
require('./include/config.php');
require('./include/init.php');
require('./include/public_fun.php');
/**
* 上传手册
*/
class Upload extends Public_fun
{
/**
* 手册文件
*/
public array $file;
/**
* 访问类型
*/
public string $action;
/**
* 手册内容
*/
public string $text;
/**
* 当前脚本相对路径
*/
public const PATH = '/upload.php';
public function __construct()
{
$this->must_login();
$this->action = $_POST['action'] ?? '';
if ($this->action == 'submit') {
$this->submit();
header('location: ./');
die();
}
}
/**
* 提交事件
*/
public function submit()
{
// 获取文件数据
$this->file = $_FILES['file'] ?? null;
if (!$this->file || $this->file['error'] > 0) {
die('文件上传失败');
}
// 获取手册名称
$this->get_title();
// 判断文件大小是否超出限制
if ($this->file['size'] > 3e6) {
die('文件大小不能超过 3 MB');
}
$this->get_intro();
$this->read_file();
$this->create_book();
$this->create_article();
}
/**
* 创建文章
*/
public function create_article()
{
$list = explode('【', $this->text);
$book_id = $this->get_new_book_id();
$table = Config::$table['article'];
// 获取日期时间,这里不使用数据库的 CURRENT_TIMESTAMP 是为了保证所有文章上传时的时间的一致
ini_set('date.timezone', 'Asia/Shanghai');
$datetime = date("Y-m-d H:i:s");
for ($x = 1; $x < count($list); $x++) {
$item = explode('】', $list[$x]);
if (count($item) != 2) {
continue;
}
$title = addslashes(trim($item[0]));
$content = $this->parseContent(trim($item[1]));
$sql = "INSERT INTO `$table` (`title`, `content`, `book_id`, `update_time`) VALUES ('$title', '$content', '$book_id', '$datetime');";
mysqli_query(Init::$conn, $sql);
}
}
public function parseContent(string $content): string
{
$content = str_replace('&left;', '【', $content);
$content = str_replace('&right;', '】', $content);
$content = addslashes($content);
return $content;
}
/**
* 读取手册文本文件
*/
public function read_file()
{
$file_path = 'book_' . str_shuffle(time()) . '.txt';
$move = move_uploaded_file($this->file['tmp_name'], $file_path);
if (!$move) {
die('上传失败,请检查是否开启写入权限');
}
$text = file_get_contents($file_path);
unlink($file_path);
$this->text = $text;
}
}
new Upload();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<?php require('./include/head.php') ?>
<title>上传手册</title>
<script>
const PAGE_NAME = 'upload' // 页面标识
function changeFile() {
let dom = document.querySelector('#bookFile')
let files = dom.files
if (files.length != 0 & files[0].size > 3e6) {
alert('文件大小不能超过 3 MB')
dom.value = null
}
}
</script>
</head>
<body>
<?php require('./include/nav.php') ?>
<div class="container">
<div class="row">
<div class="col-xxl-6 col-xl-7 col-lg-8 mx-auto">
<form method="POST" enctype="multipart/form-data">
<h4 class="mb-3">手册上传</h4>
<div class="mb-3">
<label for="bookName" class="form-label">手册名称(250字以内)</label>
<input type="text" class="form-control" name="title" id="bookName" placeholder="请输入手册名称" required>
</div>
<div class="mb-3">
<label for="bookFile" class="form-label">选择手册文件</label>
<input type="file" class="form-control" name="file" id="bookFile" accept="text/plain" required onchange="changeFile()">
<div class="form-text">文件格式要求为 txt 格式,编码要求为 UTF-8</div>
</div>
<div class="mb-3">
<label for="bookIntro" class="form-label">手册介绍(500 字以内)</label>
<textarea class="form-control" name="intro" rows="5" id="bookIntro" placeholder="这是一本神奇的手册..." required></textarea>
</div>
<input type="hidden" name="action" value="submit">
<input type="submit" class="btn btn-success mb-3" value="提交">
</form>
</div>
</div>
</div>
<script>
function limitInputNum(element, num) {
element.addEventListener('keyup', function(event) {
if (element.value.length >= num) {
element.value = element.value.substr(0, num)
}
})
}
let introDom = document.getElementById('bookIntro')
let nameDom = document.getElementById('bookName')
limitInputNum(nameDom, 250)
limitInputNum(introDom, 500)
</script>
<?php require('./include/footer.php') ?>
</body>
</html>