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 小时前
Flutter 伪3D绘制#03 | 轴测投影原理分析
android·flutter·canvas
我不会编程5555 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
李少兄5 小时前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
无名之逆6 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust
似水এ᭄往昔6 小时前
【C语言】文件操作
c语言·开发语言
啊喜拔牙6 小时前
1. hadoop 集群的常用命令
java·大数据·开发语言·python·scala
xixixin_6 小时前
为什么 js 对象中引用本地图片需要写 require 或 import
开发语言·前端·javascript
W_chuanqi6 小时前
安装 Microsoft Visual C++ Build Tools
开发语言·c++·microsoft
anlogic6 小时前
Java基础 4.3
java·开发语言
omegayy7 小时前
Unity 2022.3.x部分Android设备播放视频黑屏问题
android·unity·视频播放·黑屏