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);
}
相关推荐
l1t29 分钟前
分析xml标签属性和压缩级别对xlsx文件读取解析的影响
xml·开发语言·python·sql·duckdb
Jenkinscao41 分钟前
我从零开始学习C语言(13)- 循环语句 PART2
c语言·开发语言·学习
王伯爵1 小时前
go语言中的select的用法和使用场景
开发语言·数据库·golang
Chandler_Song1 小时前
【Python代码】谷歌专利CSV处理函数
开发语言·python·pandas
我是一只菜菜1 小时前
中国大学MOOC--C语言第十一周结构类型
c语言·开发语言
新子y1 小时前
【操作记录】我的 MNN Android LLM 编译学习笔记记录(一)
android·学习·mnn
源代码•宸3 小时前
网络流量分析——基础知识(二)(Tcpdump 基础知识)
运维·开发语言·网络·c++·经验分享·tcpdump
lincats3 小时前
一步一步学习使用FireMonkey动画(1) 使用动画组件为窗体添加动态效果
android·ide·delphi·livebindings·delphi 12.3·firemonkey
想想吴4 小时前
Android.bp 基础
android·安卓·android.bp
fs哆哆10 小时前
在VB.net中一维数组,与VBA有什么区别
java·开发语言·数据结构·算法·.net