文章首发及后续更新:https://mwhls.top/4092.html,无图/无目录/格式错误/更多相关请至首发页查看。
新的更新内容请到mwhls.top查看。
欢迎提出任何疑问及批评,非常感谢!
考虑到 WordPress 会自动适应界面大小,我对 html 也不熟,所以用 WordPress 创建三个栏目,参考一下它的 html。
参考了一些教程,自己写了个,代码见 附录-文中代码 中对应标题。
效果如下,始终三栏,图片自适应窗口大小,但字体大小无自适应:
但后面在改变视窗尺寸的测试中发现,当视窗比较大的时候,右边会出现空缺,而空缺会被下一行的东西补上,很丑,不过不碍事,加点占位符就解决了。
明确功能:
输出:
1655796204_before.png
delete 1655796204_before.png
1655822379_after.png
1655822379_before.png
save as./upload/1655822379_before.png ./upload/1655822379_after.png思考:
这部分写的脑袋疼,路径太奇怪了,同一个文件居然有两类路径。
实现思路:
实现效果:
结果展示
![]() | ![]() |
|---|---|
| 原始页面 | 仅上传一张图片 |
";
?>
";
?>- pdrs
--- upload.php
--- utils.php
--- upload
--- 1655820968_before.png
--- 1655809752_before.png
--- 1655796204_before.png
--- 1655822379_before.png
--- 1655822379_after.png$upload_path = ‘./upload/’;
f
i
l
e
s
i
z
e
m
a
x
=
1024
∗
1024
∗
10
;
/
/
10
M
c
h
e
c
k
o
l
d
f
i
l
e
i
n
d
i
r
(
file_size_max = 1024 * 1024 * 10; // 10M check_old_file_in_dir(
filesizemax=1024∗1024∗10;//10Mcheckoldfileindir(upload_path);
if ($_SERVER[“REQUEST_METHOD”] == “POST”)
{
// $workplace = ‘/home/ftp/y/yupfyfel/wwwroot/external-functions/pdrs/’;
$tmp_file_path_before = $upload_path . $_POST[“timestamp”] . “_before.png”;
$tmp_file_path_after = $upload_path .
P
O
S
T
[
"
t
i
m
e
s
t
a
m
p
"
]
.
"
a
f
t
e
r
.
p
n
g
"
;
m
o
v
e
u
p
l
o
a
d
e
d
f
i
l
e
(
_POST["timestamp"] . "_after.png"; move_uploaded_file(
POST["timestamp"]."after.png";moveuploadedfile(_FILES[“file1”][“tmp_name”],
t
m
p
f
i
l
e
p
a
t
h
b
e
f
o
r
e
)
;
m
o
v
e
u
p
l
o
a
d
e
d
f
i
l
e
(
tmp_file_path_before); move_uploaded_file(
tmpfilepathbefore);moveuploadedfile(_FILES[“file2”][“tmp_name”], $tmp_file_path_after);
echo “save as” . $tmp_file_path_before . " " . $tmp_file_path_after;
}
?>
function delete_old_file($file_path) {
# file_path: timestamp_filename.png
# if timestamp is ten minutes ago, delete the file
f
i
l
e
n
a
m
e
=
s
u
b
s
t
r
(
file_name = substr(
filename=substr(file_path, strrpos($file_path, ‘/’) + 1);
t
i
m
e
s
t
a
m
p
=
s
u
b
s
t
r
(
timestamp = substr(
timestamp=substr(file_name, 0, strrpos($file_name, ‘_’));
$timestamp_now = time();
$timestamp_ago =
t
i
m
e
s
t
a
m
p
n
o
w
−
600
;
i
f
(
timestamp_now - 600; if (
timestampnow−600;if(timestamp < KaTeX parse error: Expected '}', got 'EOF' at end of input: … unlink(file_path);
return true;
}
return false;
}
function check_old_file_in_dir($dir) {
# dir: ./upload/
f
i
l
e
s
=
s
c
a
n
d
i
r
(
files = scandir(
files=scandir(dir);
foreach ($files as KaTeX parse error: Expected '}', got 'EOF' at end of input: … { if (file != ‘.’ && $file != ‘…’) {
echo KaTeX parse error: Expected 'EOF', got '&' at position 9: file . '&̲lt;br>'; …dir . $file)) {
echo 'delete ’ . $file . ‘
’;
}
}
}
}
?>
$timestamp = time();
$file_name_before = $timestamp . ‘_before.png’;
$file_name_after = $timestamp . ‘_after.png’;
$html = ’
';
echo $html;
?>
- pdrs
--- upload.php
--- utils.phpif ($_SERVER[“REQUEST_METHOD”] == “POST”){
$file_before = $_FILES[“file_before”];
$file_after = $_FILES[“file_after”];
if (check_file($file_before)){
# save img
// $file_path_before = $upload_path . $_POST['file_name_before'];
// move_uploaded_file($file_before["tmp_name"], $file_path_before);
# img to base64
$img_before = img_to_base64($file_before["tmp_name"]);
}
if (check_file($file_after)){
# save img
// $file_path_after = $upload_path . $_POST['file_name_after'];
// move_uploaded_file($file_after["tmp_name"], $file_path_after);
# img to base64
$img_after = img_to_base64($file_after["tmp_name"]);
}
}
$html = ’
$file_size_max = 1024 * 1024 * 2; // 2M
function print_file_in_dir($dir) {
# dir: ./upload/
f
i
l
e
s
=
s
c
a
n
d
i
r
(
files = scandir(
files=scandir(dir);
foreach ($files as KaTeX parse error: Expected '}', got 'EOF' at end of input: … { if (file != ‘.’ && $file != ‘…’) {
echo(“file:” . $dir . ‘/’ . $file . “
”);
}
}
}
function delete_old_file($file_path) {
# file_path: timestamp_filename.png
# if timestamp is ten minutes ago, delete the file
f
i
l
e
n
a
m
e
=
s
u
b
s
t
r
(
file_name = substr(
filename=substr(file_path, strrpos($file_path, ‘/’) + 1);
t
i
m
e
s
t
a
m
p
=
s
u
b
s
t
r
(
timestamp = substr(
timestamp=substr(file_name, 0, strrpos($file_name, ‘_’));
$timestamp_now = time();
$timestamp_ago =
t
i
m
e
s
t
a
m
p
n
o
w
−
600
;
i
f
(
timestamp_now - 600; if (
timestampnow−600;if(timestamp < KaTeX parse error: Expected '}', got 'EOF' at end of input: … unlink(file_path);
return true;
}
return false;
}
function check_old_file_in_dir($dir) {
# dir: ./upload/
f
i
l
e
s
=
s
c
a
n
d
i
r
(
files = scandir(
files=scandir(dir);
foreach ($files as KaTeX parse error: Expected '}', got 'EOF' at end of input: … { if (file != ‘.’ && KaTeX parse error: Expected '}', got 'EOF' at end of input: …elete_old_file(dir . $file);
}
}
}
function test_input($data){
# copy from: https://www.runoob.com/php/php-form-validation.html
d
a
t
a
=
t
r
i
m
(
data = trim(
data=trim(data);
d
a
t
a
=
s
t
r
i
p
s
l
a
s
h
e
s
(
data = stripslashes(
data=stripslashes(data);
d
a
t
a
=
h
t
m
l
s
p
e
c
i
a
l
c
h
a
r
s
(
data = htmlspecialchars(
data=htmlspecialchars(data);
return $data;
}
function check_file($_file) {
# check file type, size. Allow type: png, jpg, jpeg
# $_file: $_FILES[“file”]
$file_type = $_file[“type”];
$file_size =
f
i
l
e
[
"
s
i
z
e
"
]
;
i
f
(
_file["size"]; if (
file["size"];if(file_type != “image/png” && $file_type != “image/jpg” && $file_type != “image/jpeg” || $file_size > $GLOBALS[‘file_size_max’]) {
echo "文件类型或大小不符合要求: " . $file_type . " " . $file_size;
return false;
}
return true;
}
function img_to_base64(KaTeX parse error: Expected '}', got '#' at position 18: …le_path) { #̲ file_path: ./u…fp = fopen($file_path, “r”)) {
c
o
n
t
e
n
t
=
f
r
e
a
d
(
content = fread(
content=fread(fp, filesize(
f
i
l
e
p
a
t
h
)
)
;
f
c
l
o
s
e
(
file_path)); fclose(
filepath));fclose(fp);
f
i
l
e
b
a
s
e
64
=
′
d
a
t
a
:
i
m
a
g
e
/
p
n
g
;
b
a
s
e
64
,
′
.
b
a
s
e
6
4
e
n
c
o
d
e
(
file_base64 = 'data:image/png;base64,' . base64_encode(
filebase64=′data:image/png;base64,′.base64encode(content);
return $file_base64;
}
return false;
}