-
Notifications
You must be signed in to change notification settings - Fork 1
/
chunk_uploader.php
57 lines (48 loc) · 1.4 KB
/
chunk_uploader.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
56
57
<?php
if(isset($_GET['path'])){
$path = $_GET['path']."/";
}else{
$path = "uploads/";
}
// RESPONSE FUNCTION
function verbose($ok=1,$info=""){
// THROW A 400 ERROR ON FAILURE
if ($ok==0) { http_response_code(400); }
die(json_encode(["ok"=>$ok, "info"=>$info]));
}
// INVALID UPLOAD
if (empty($_FILES) || $_FILES['file']['error']) {
verbose(0, "Failed to move uploaded file.");
}
// THE UPLOAD DESITINATION - CHANGE THIS TO YOUR OWN
$filePath = $path;
if (!file_exists($filePath)) {
if (!mkdir($filePath, 0777, true)) {
verbose(0, "Failed to create $filePath");
}
}
$fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : $_FILES["file"]["name"];
$filePath = $filePath . DIRECTORY_SEPARATOR . $fileName;
// DEAL WITH CHUNKS
$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
$chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
$out = @fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");
if ($out) {
$in = @fopen($_FILES['file']['tmp_name'], "rb");
if ($in) {
while ($buff = fread($in, 4096)) { fwrite($out, $buff); }
} else {
verbose(0, "Failed to open input stream");
}
@fclose($in);
@fclose($out);
@unlink($_FILES['file']['tmp_name']);
} else {
verbose(0, "Failed to open output stream");
}
// CHECK IF FILE HAS BEEN UPLOADED
if (!$chunks || $chunk == $chunks - 1) {
rename("{$filePath}.part", $filePath);
}
verbose(1, "Upload OK");
?>