first commit
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
defined('BASEPATH') or exit('No direct script access allowed');
|
||||
|
||||
const TEMP_DIRECTORY_UPLOAD = 'application/cache/upload';
|
||||
|
||||
// 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]));
|
||||
}
|
||||
|
||||
function upload_file()
|
||||
{
|
||||
// INVALID UPLOAD
|
||||
if (empty($_FILES) || $_FILES['file']['error']) {
|
||||
verbose(0, "Failed to move uploaded file.");
|
||||
}
|
||||
|
||||
// THE UPLOAD DESITINATION - CHANGE THIS TO YOUR OWN
|
||||
$filePath = $_SERVER['DOCUMENT_ROOT']
|
||||
. DIRECTORY_SEPARATOR
|
||||
. TEMP_DIRECTORY_UPLOAD;
|
||||
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");
|
||||
}
|
||||
Reference in New Issue
Block a user