文章首发及后续更新:https://mwhls.top/4092.html,无图/无目录/格式错误/更多相关请至首发页查看。
新的更新内容请到mwhls.top查看。
欢迎提出任何疑问及批评,非常感谢!
目录 框架搭建 测试 – 图片展示栏 测试 – 图片上传与自动删除 测试 – 上传与处理 附录 参考文献 文中代码 测试 – 图片展示栏 测试 – 图片上传与自动删除 测试 – 上传与处理飞桨模型部署至docker并使用FastAPI调用
考虑到 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/’;
filesizemax=1024∗1024∗10;//10Mcheckoldfileindir(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 . POST["timestamp"]."after.png";moveuploadedfile(_POST["timestamp"] . "_after.png"; move_uploaded_file(POST["timestamp"]."after.png";moveuploadedfile(_FILES[“file1”][“tmp_name”], tmpfilepathbefore);moveuploadedfile(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
filename=substr(file_name = substr(filename=substr(file_path, strrpos($file_path, ‘/’) + 1);
timestamp=substr(timestamp = substr(timestamp=substr(file_name, 0, strrpos($file_name, ‘_’));
$timestamp_now = time();
$timestamp_ago = timestampnow−600;if(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/
files=scandir(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.php
if ($_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/
files=scandir(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
filename=substr(file_name = substr(filename=substr(file_path, strrpos($file_path, ‘/’) + 1);
timestamp=substr(timestamp = substr(timestamp=substr(file_name, 0, strrpos($file_name, ‘_’));
$timestamp_now = time();
$timestamp_ago = timestampnow−600;if(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/
files=scandir(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
data=trim(data = trim(data=trim(data);
data=stripslashes(data = stripslashes(data=stripslashes(data);
data=htmlspecialchars(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 = file["size"];if(_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”)) {
content=fread(content = fread(content=fread(fp, filesize(filepath));fclose(file_path)); fclose(filepath));fclose(fp);
filebase64=′data:image/png;base64,′.base64encode(file_base64 = 'data:image/png;base64,' . base64_encode(filebase64=′data:image/png;base64,′.base64encode(content);
return $file_base64;
}
return false;
}
上一篇:医学上ECG是指什么
下一篇:下来的反义词是什么