豌豆Ai站群搜索引擎系统 V.25.10.25 网址:www.wd.chat

🗣 Wd提问: php ajax 文件上传

🤖 Ai回答:
php
<?php
// upload.php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
$uploadDir = 'uploads/';
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0777, true);
}

$fileName = basename($_FILES['file']['name']);
$targetPath = $uploadDir 、$fileName;
$uploadOk = 1;
$fileType = strtolower(pathinfo($targetPath, PATHINFO_EXTENSION));

// 允许的文件类型
$allowedTypes = ['jpg', 'jpeg', 'png', 'gif', 'pdf', 'docx'];
if (!in_array($fileType, $allowedTypes)) {
echo json_encode(['status' = 'error', 'message' = '不支持的文件类型。']);
exit;
}

// 检查文件大小 (例如:限制5MB)
if ($_FILES['file']['size'] 5 * 1024 * 1024) {
echo json_encode(['status' = 'error', 'message' = '文件太大,不能超过5MB。']);
exit;
}

if (move_uploaded_file($_FILES['file']['tmp_name'], $targetPath)) {
echo json_encode([
'status' = 'success',
'message' = '文件上传成功。',
'file' = $targetPath
]);
} else {
echo json_encode(['status' = 'error', 'message' = '文件上传失败。']);
}
}
?

html
<!DOCTYPE html
<html lang=zh
<head
<meta charset=UTF-8
<titleAJAX 文件上传</title
</head
<body
<form id=uploadForm enctype=multipart/form-data
<input type=file name=file id=fileInput required /
<button type=submit上传文件</button
</form

<div id=result</div

<script
document.getElementById('uploadForm').addEventListener('submit', function(e) {
e.preventDefault();

const formData = new FormData();
const fileInput = document.getElementById('fileInput');
formData.append('file', fileInput.files[0]);

fetch('upload.php', {
method: 'POST',
body: formData
})
.then(response = response.json())
.then(data = {
const resultDiv = document.getElementById('result');
if (data.status === 'success') {
resultDiv.innerHTML = `<p style=color:green;${data.message}</p`;
} else {
resultDiv.innerHTML = `<p style=color:red;错误:${data.message}</p`;
}
})
.catch(error = {
document.getElementById('result').innerHTML = `<p style=color:red;请求失败:${error}</p`;
});
});
</script
</body
</html

使用说明:
1、将 PHP 代码保存为 `upload.php`。
2、将 HTML + JS 代码保存为 `index.html`。
3、确保服务器支持 PHP,并创建 `uploads/` 目录(或确保其可写)。
4、访问 `index.html` 即可测试 AJAX 文件上传功能。

安全建议:
验证文件类型和扩展名。
限制文件大小。
对上传目录进行权限控制。
生产环境应增加更多安全校验(如 MIME 类型检查、防病毒扫描等)。

Ai作答

📣 商家广告


贵阳用友软件

广告招商

香港vps

广告招商

月饼


0

IP地址: 31.59.205.82

搜索次数: 6

提问时间: 2025-11-12 20:29:16

🛒 域名购买
2.sd.cn
32.cx
67.plus
app.ln.cn
pai.show
ty.gs
zd.gs
38.gold
dttt.com.cn
94.gold

❓️ 热门提问
独立服务器的
申请us域名
香港的房价均价多少
注册的域名有什么用
com 域名抢注
云服务器接入商
湖北十堰云服务器
蜘蛛池自动站群跳转
网站推广用户转介绍
cloudn vps
豌豆Ai站群搜索引擎系统

🌐 域名评估
okhv.cn
evne.cn
bb.hk.cn
tt.jl.cn
ss.nx.cn
xphone.green
v.qh.cn
wg.gs
pp.jl.cn
ypettoy.com

⛏ 最新挖掘
凯里酸汤鱼
贵阳烤脑花
都匀毛尖茶
花溪牛肉粉
贵阳烤生蚝
贵阳烤豆腐
贵阳纸包鱼
贵阳特色菜

🖌 热门作画

🤝 关于我们:
豌豆Ai 域名 建站 站群 留痕 推广 评估 源码
开发Ai 工具 日记 价格 加盟 广告 流量 留言 联系

🗨 加入群聊
群

🔗 友情链接
站群软件  全球搜索引擎  ai提问

🧰 站长工具
Ai工具  whois查询  搜索

📢 温馨提示:本站所有问答由Ai自动创作,内容仅供参考,若有误差请用“联系”里面信息通知我们人工修改或删除。

👉 技术支持:本站由豌豆Ai提供技术支持,使用的最新版:《豌豆Ai站群搜索引擎系统 V.25.10.25》搭建本站。

上一篇 93086 93087 93088 下一篇