-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.php
56 lines (46 loc) · 1.38 KB
/
upload.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
<?php
// Disabled cache
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
exit;
}
// Settings
$targetDir = FILE_UPLOAD_DIR;
$maxFileAge = 5 * 3600; // Temp file age in seconds
// Create target dir
if (!file_exists($targetDir)) {
@mkdir($targetDir);
}
// Get a file name
if (isset($_REQUEST["name"])) {
$fileName = $_REQUEST["name"];
} elseif (!empty($_FILES)) {
$fileName = $_FILES["file"]["name"];
}
// Chunking might be enabled
$chunk = intval($_REQUEST["chunk"]) > 0 ? intval($_REQUEST["chunk"]) : 1;
$chunks = intval($_REQUEST["chunks"]) > 0 ? intval($_REQUEST["chunks"]) : 1;
$filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
move_uploaded_file($_FILES["file"]["tmp_name"], $filePath . '-' . $chunk . '.part');
sleep(1); // Give it a moment
// Check if file has been uploaded
if (!$chunks || $chunk == $chunks) {
$out = @fopen($filePath, 'wb');
for ($i = 0; $i < $chunks; $i++) {
$in = @fopen($filePath . '-' . $i . '.part', 'rb');
stream_copy_to_stream($in, $out);
@fclose($in);
unlink($filePath . '-' . $i . '.part');
}
@fclose($out);
}
// Return Success Response
die(json_encode(array(
'filename' => $fileName,
'chunk' => $chunk,
'chunks' => $chunks,
)));
?>