Deal chunk uploaded files in php

房东的猫 提交于 2019-12-13 02:17:27

问题


I'm using angular ng-upload to upload file, here's my javascript code:

$scope.uploadFile = function(file) {
    Upload.upload({
        url: '/upload_image',
        resumeChunkSize: '1MB',
        data: {file: file}
    }).then(function(response) {
        console.log('success');
    });
};

Since I'm uploading file in chunks, I want the backend php to read and concatenate those chunks back, now how do I do that? My current php code looks like this:

$filename = $_FILES['file']['name'];
$destination = '/someDestinationPath/' . $filename;
move_uploaded_file( $_FILES['file']['tmp_name'] , $destination );

Yet I am pretty sure this doesn't work for chunk uploads...


回答1:


Ok, I've figured. When using chunk uploading function of some javascript libraries, they have parameters passed to backend about the chunk info. In the case of ng-file-upload, these parameters are _chunkNumber, _chunkSize and _totalSize. I believe the same thing for Plupload. Do some math you'll know how many chunks are needed for this file to finish uploading. For each chunk uploading, there will be one http post request sent to backend PHP. Enough talking, code here:

PHP:

    // uid is used to identify chunked partial files so we can assemble them back when all chunks are finished uploading
    $uid = $_REQUEST['uid'];
    $filename = $_REQUEST['filename'];
    if (empty($_FILES['file']['name'])) {
        return '';
    }
    if (isset($_POST['_chunkNumber'])) {
        // the file is uploaded piece by piece, chunk mode
        $current_chunk_number = $_REQUEST['_chunkNumber'];
        $chunk_size = $_REQUEST['_chunkSize'];
        $total_size = $_REQUEST['_totalSize'];

        $upload_folder = base_path() . '/public/images/uploaded/';
        $total_chunk_number = ceil($total_size / $chunk_size);
        move_uploaded_file($_FILES['file']['tmp_name'], $upload_folder . $uid . '.part' . $current_chunk_number);
        // the last chunk of file has been received
        if ($current_chunk_number == ($total_chunk_number - 1)) {
            // reassemble the partial pieces to a whole file
            for ($i = 0; $i < $total_chunk_number; $i ++) {
                $content = file_get_contents($upload_folder . $uid . '.part' . $i);
                file_put_contents($upload_folder . $filename, $content, FILE_APPEND);
                unlink($upload_folder . $uid . '.part' . $i);
            }

        }
    } else {
        // the file is uploaded as a whole, no chunk mode
        move_uploaded_file($_FILES['file']['tmp_name'], $upload_folder . $filename);
    }

Javascript(angularjs and ng-file-upload):

Upload.upload({
    url: '/upload_image',
    resumeChunkSize: '500KB',
    data: {
        filename: file.name
        file: file
        uid: toolbox.generateUniqueID()
    }
}).then(function(response) {
    console.log('success');
}), function(response) {
    console.log('error');
});


来源:https://stackoverflow.com/questions/37007240/deal-chunk-uploaded-files-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!