如何使用ng-file-upload在PHP中处理块上传?

问题描述 投票:0回答:1

我尝试实现一个PHP脚本,该脚本支持使用ng-file-upload上传块。 有人有主意吗?

ng-file-upload
1个回答
0
投票

当然,这是我在Google上找到的要点:

https://gist.github.com/jayarjo/5846636

<?php

if (empty($_FILES) || $_FILES['file']['error']) {
    die('{"OK": 0, "info": "Failed to move uploaded file."}');
}

$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
$chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;

$fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : $_FILES["file"]["name"];
$filePath = "uploads/$fileName";


// Open temp file
$out = @fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");
if ($out) {
    // Read binary input stream and append it to temp file
    $in = @fopen($_FILES['file']['tmp_name'], "rb");

    if ($in) {
        while ($buff = fread($in, 4096))
            fwrite($out, $buff);
    } else
        die('{"OK": 0, "info": "Failed to open input stream."}');

    @fclose($in);
    @fclose($out);

    @unlink($_FILES['file']['tmp_name']);
} else
    die('{"OK": 0, "info": "Failed to open output stream."}');


// Check if file has been uploaded
if (!$chunks || $chunk == $chunks - 1) {
    // Strip the temp .part suffix off 
    rename("{$filePath}.part", $filePath);
}

die('{"OK": 1, "info": "Upload successful."}');
© www.soinside.com 2019 - 2024. All rights reserved.