forked from boundstate/yii2-plupload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChunkUploader.php
55 lines (45 loc) · 1.52 KB
/
ChunkUploader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
namespace boundstate\plupload;
use Yii;
use yii\base\Exception;
use yii\web\UploadedFile;
class ChunkUploader
{
/**
* Processes a chunked file upload.
* @param UploadedFile $uploadedFile
* @param string $path path to write chunks to
* @returns boolean true if file upload is complete, or false if there are more chunks
* @throws Exception
*/
public static function process($uploadedFile, $path) {
if (!$uploadedFile || $uploadedFile->hasError) {
throw new Exception('Failed to upload file');
}
$chunk = (int)Yii::$app->request->getBodyParam('chunk', 0);
$totalChunks = (int)Yii::$app->request->getBodyParam('chunks', 0);
$out = fopen("$path.part", $chunk == 0 ? 'wb' : 'ab');
if (!$out) {
throw new Exception('Failed to open output stream');
}
// Read binary input stream and append it to temporary .part file
$in = fopen($uploadedFile->tempName, 'rb');
if ($in) {
while ($buff = fread($in, 4096)) {
fwrite($out, $buff);
}
} else {
throw new Exception('Failed to open input stream');
}
fclose($in);
fclose($out);
unlink($uploadedFile->tempName);
// Check if all chunks have been processed
if (!$totalChunks || $chunk == $totalChunks - 1) {
// Strip the temp .part suffix off
rename("$path.part", $path);
return true;
}
return false;
}
}