PHP 8.5 尚未发布,当前最新稳定版为 PHP 8.3;推荐使用 PHP 8.0+ 原生 session.upload_progress 方案实现上传进度条,无需额外扩展,兼容 Apache/Nginx+PHP-FPM。

PHP 8.5 尚未发布(截至 2026 年 9 月,官方最新稳定版为 PHP 8.3,PHP 8.4 处于 RC 阶段),因此 不存在 PHP 8.5 的正式版本或对应配置。你可能是指:
- ✅ 当前实际使用的 PHP 版本(如 8.1 / 8.2 / 8.3)
- ✅ 或希望在较新 PHP 环境(≥ 8.0)中实现上传进度条
以下内容基于 PHP 8.0+ 稳定环境,完全兼容 PHP 8.1–8.3,并适用于主流部署方式(Apache/Nginx + PHP-FPM)。不依赖已废弃扩展(如 APC),推荐现代、安全、免编译的方案。
用 Session 上传进度(PHP 8.0+ 原生支持,最推荐)
这是目前最简洁、无需额外模块、且与 PHP 8 兼容性最好的方式。
1. 启用上传进度跟踪
在 php.ini 中确认开启(默认 PHP 5.4+ 已启用,但需检查):
session.upload_progress.enabled = On session.upload_progress.name = "PHP_SESSION_UPLOAD_PROGRESS" session.upload_progress.freq = "100%" session.upload_progress.min_freq = "1" session.upload_progress.cleanup = On
✅ 修改后重启 PHP-FPM 和 Web 服务器(如
sudo systemctl restart php8.3-fpm nginx)立即学习“PHP免费学习笔记(深入)”;
2. 前端表单添加唯一标识字段
<form id="uploadForm" action="upload.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="<?php echo ini_get('session.upload_progress.name'); ?>" value="my_upload_123">
<input type="file" name="myfile" required>
<button type="submit">上传</button>
</form>
<div class="progress-bar">
<div id="progress" style="width: 0%; background: #4CAF50; height: 20px;"></div>
</div>
<p id="progress-text">等待上传...</p>3. 创建进度查询接口(progress.php)
<?php
session_start();
$key = 'upload_progress_' . ($_GET['key'] ?? 'my_upload_123');
if (isset($_SESSION[$key])) {
$progress = $_SESSION[$key];
$done = $progress['done'] ?? false;
$percent = $done ? 100 : round(($progress['bytes_uploaded'] / $progress['bytes_total']) * 100);
header('Content-Type: application/json');
echo json_encode([
'percent' => min(100, max(0, $percent)),
'done' => $done,
'total' => $progress['bytes_total'] ?? 0,
'uploaded'=> $progress['bytes_uploaded'] ?? 0
]);
} else {
http_response_code(404);
echo json_encode(['percent' => 0, 'done' => false]);
}4. 前端轮询获取进度(JS)
document.getElementById('uploadForm').addEventListener('submit', function(e) {
e.preventDefault();
const formData = new FormData(this);
const xhr = new XMLHttpRequest();
// 发起上传(会自动触发 session 进度记录)
xhr.open('POST', 'upload.php');
xhr.upload.onprogress = function(e) {
if (e.lengthComputable) {
const percent = (e.loaded / e.total) * 100;
updateProgress(percent);
}
};
xhr.onload = function() {
if (xhr.status === 200) updateProgress(100);
};
xhr.send(formData);
// 同时启动轮询(防 upload.onprogress 不触发时兜底)
const poll = setInterval(() => {
fetch(`progress.php?key=my_upload_123`)
.then(r => r.json())
.then(data => {
updateProgress(data.percent);
if (data.done) clearInterval(poll);
});
}, 500);
});
function updateProgress(p) {
document.getElementById('progress').style.width = p + '%';
document.getElementById('progress-text').textContent = `上传中:${Math.round(p)}%`;
}纯前端监听(零服务端改造,HTML5 标准方案)
适用于现代浏览器(Chrome/Firefox/Edge ≥ 2020),无需任何 PHP 配置变更。
const fileInput = document.querySelector('input[type="file"]');
fileInput.addEventListener('change', function() {
const file = this.files[0];
if (!file) return;
const formData = new FormData();
formData.append('myfile', file);
const xhr = new XMLHttpRequest();
xhr.open('POST', 'upload.php');
// 关键:监听上传过程
xhr.upload.addEventListener('progress', function(e) {
if (e.lengthComputable) {
const percent = (e.loaded / e.total) * 100;
document.getElementById('progress').style.width = percent + '%';
document.getElementById('progress-text').textContent = `客户端上传:${Math.round(percent)}%`;
}
});
xhr.addEventListener('load', function() {
if (xhr.status === 200) {
document.getElementById('progress').style.width = '100%';
document.getElementById('progress-text').textContent = '上传成功!';
}
});
xhr.send(formData);
});⚠️ 注意:此方式仅监控「浏览器到服务器」的网络传输进度,不包含 PHP 后端处理时间(如 move_uploaded_file、图像压缩等),但响应最快、最轻量。
避坑提醒(PHP 8 环境特别注意)
- ❌ 不要尝试 APC 或
uploadprogress扩展:APC 在 PHP 5.5+ 已被 OPcache 取代;uploadprogress无 PHP 8 兼容版本,编译失败率高。 - ❌ Nginx
upload_progress_module需手动编译,PHP 8.3+ 下维护困难,不推荐新项目使用。 - ✅ Session 方案在 PHP 8.1+ 经充分测试,
session.upload_progress.*参数全部保留且行为稳定。 - ✅ 确保
session_start()在progress.php开头调用,且 session 存储路径可写(检查session.save_path)。 - ✅ 大文件上传务必同步调整
php.ini:upload_max_filesize = 512M post_max_size = 512M max_execution_time = 3600 max_input_time = 3600
不需要新版本 PHP,也不需要复杂扩展。PHP 8.0+ 环境下,用 Session 进度 + 前端轮询或原生 upload.onprogress,就能稳定跑出准确进度条。



















