在PHP新政块上传的文件 [英] Deal chunk uploaded files in php

查看:272
本文介绍了在PHP新政块上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的角度NG-上传来上传文件,这里是我的JavaScript code:

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');
    });
};

由于我在上传文件块,我希望后端PHP读取并串连这些块回来,现在我该怎么做呢?我当前的PHP code是这样的:

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 );

但我是pretty确保这不会对大块的上传工作...

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

推荐答案

好吧,我已经想通。当使用一些JavaScript库的大块上传功能,他们传递给后端的有关信息块参数。以ng-文件上传的情况下,这些参数是_chunkNumber,_chunkSize和_totalSize。我相信Plupload同样的事情。做一些数学,你会知道有多少块都需要这个文件来完成上传。对于每一个组块上载,将有发送到后端的PHP的HTTP post请求。够交谈,code位置:

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和NG-文件上传):

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');
});

这篇关于在PHP新政块上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆