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);
}
相关推荐
明月看潮生4 分钟前
青少年编程与数学 02-019 Rust 编程基础 14课题、并发编程
开发语言·青少年编程·rust·编程与数学
難釋懷12 分钟前
Android开发-Application
android
Warren9815 分钟前
Java面试八股Spring篇(4500字)
java·开发语言·spring boot·后端·spring·面试
晚秋大魔王21 分钟前
OpenHarmony 开源鸿蒙南向开发——linux下使用make交叉编译第三方库——gnutls
java·开发语言
EelBarb22 分钟前
python:一个代理流量监控的媒体文件下载脚本
开发语言·python
背帆32 分钟前
go的interface接口底层实现
开发语言·后端·golang
小屁孩大帅-杨一凡44 分钟前
一个简单点的js的h5页面实现地铁快跑的小游戏
开发语言·前端·javascript·css·html
顾子茵1 小时前
c++从入门到精通(四)--动态内存,模板与泛型编程
java·开发语言·c++
电信2301杨臣1 小时前
QT---信号与槽
开发语言·qt
明月看潮生1 小时前
青少年编程与数学 02-019 Rust 编程基础 07课题、字符串
开发语言·青少年编程·rust·编程与数学