PHP处理大文件上传

前段HTML代码如下:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>分块上传大文件</title>
</head>
<body>
    <input type="file" id="fileInput">
    <button onclick="uploadFileInChunks()">上传</button>
    <progress id="progressBar" value="0" max="100"></progress>

    <script>
        const CHUNK_SIZE = 1024 * 1024;  // 每个分块的大小为 1MB

        function uploadFileInChunks() {
            const fileInput = document.getElementById('fileInput');
            const file = fileInput.files[0];
            const progressBar = document.getElementById('progressBar');

            if (file) {
                let start = 0;
                let chunkIndex = 0;
                const totalChunks = Math.ceil(file.size / CHUNK_SIZE);

                function uploadChunk() {
                    const end = Math.min(start + CHUNK_SIZE, file.size);
                    const chunk = file.slice(start, end);

                    const formData = new FormData();
                    formData.append('chunk', chunk);
                    formData.append('chunkIndex', chunkIndex);
                    formData.append('totalChunks', totalChunks);
                    formData.append('fileName', file.name);

                    const xhr = new XMLHttpRequest();
                    xhr.open('POST', 'http://127.0.0.1/index.php', true);

                    xhr.onload = function () {
                        if (xhr.status === 200) {
                            start = end;
                            chunkIndex++;
                            const percentComplete = (chunkIndex / totalChunks) * 100;
                            progressBar.value = percentComplete;

                            if (start < file.size) {
                                uploadChunk();
                            } else {
                                alert('文件上传完成');
                            }
                        }
                    };

                    xhr.send(formData);
                }

                uploadChunk();
            }
        }
    </script>
</body>
</html>

后端PHP代码如下:

php 复制代码
<?php

// 设置允许跨域请求
header("Access-Control-Allow-Origin: *");

$chunk = $_FILES['chunk']['tmp_name'];
$chunkIndex = (int)$_POST['chunkIndex'];
$totalChunks = (int)$_POST['totalChunks'];
$fileName = $_POST['fileName'];

$uploadDir = './temp_uploads/';
if (!is_dir($uploadDir)) {
    mkdir($uploadDir, 0777, true);
}

$tempFilePath = $uploadDir . $fileName . '.part' . $chunkIndex;
move_uploaded_file($chunk, $tempFilePath);

if ($chunkIndex === $totalChunks - 1) {
    $outputFilePath = $uploadDir . $fileName;
    $outputFile = fopen($outputFilePath, 'wb');
    for ($i = 0; $i < $totalChunks; $i++) {
        $partFilePath = $uploadDir . $fileName . '.part' . $i;
        $partFile = fopen($partFilePath, 'rb');
        stream_copy_to_stream($partFile, $outputFile);
        fclose($partFile);
        unlink($partFilePath);
    }
    fclose($outputFile);
}
相关推荐
一念&1 小时前
每日一个C语言知识:C 头文件
c语言·开发语言·算法
DARLING Zero two♡2 小时前
仓颉GC调优参数:垃圾回收的精密控制艺术
开发语言·仓颉
今日说"法"2 小时前
Rust探秘:所有权转移在函数调用中的表现
开发语言·后端·rust
java1234_小锋2 小时前
PyTorch2 Python深度学习 - 自动微分(Autograd)与梯度优化
开发语言·python·深度学习·pytorch2
Python私教3 小时前
C 语言运算符全景:从入门到进阶
c语言·开发语言·网络
csbysj20204 小时前
Perl 格式化输出
开发语言
tao3556674 小时前
【Python刷力扣hot100】42. Trapping Rain Water
开发语言·python·leetcode
马 孔 多 在下雨4 小时前
安卓开发popupWindow的使用
android
asfdsfgas4 小时前
从 SSP 配置到 Gradle 同步:Android SDK 开发中 Manifest 合并冲突的踩坑记录
android
星光一影5 小时前
供应链进销存源码uniapp全开源ERP多仓库管理系统pc+app手机端
mysql·elementui·uni-app·开源·php·phpstorm·1024程序员节