Chunked file upload (#714)

Removes any PHP or server-side file-upload limits by using file chunks
This commit is contained in:
Daniele Paganelli 2022-02-12 08:46:07 +01:00 committed by GitHub
parent 154947ef83
commit c3a2f1d604
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@
//Default Configuration
$CONFIG = '{"lang":"en","error_reporting":false,"show_hidden":false,"hide_Cols":false,"calc_folder":false,"theme":"light"}';
/**
* H3K | Tiny File Manager V2.4.6
* CCP Programmers | ccpprogrammers@gmail.com
@ -850,6 +851,9 @@ if (isset($_GET['dl'])) {
// Upload
if (!empty($_FILES) && !FM_READONLY) {
$override_file_name = false;
$chunkIndex = $_POST['dzchunkindex'];
$chunkTotal = $_POST['dztotalchunkcount'];
$f = $_FILES;
$path = FM_ROOT_PATH;
$ds = DIRECTORY_SEPARATOR;
@ -882,20 +886,56 @@ if (!empty($_FILES) && !FM_READONLY) {
if ( is_writable($targetPath) ) {
$fullPath = $path . '/' . basename($_REQUEST['fullpath']);
$folder = substr($fullPath, 0, strrpos($fullPath, "/"));
if(file_exists ($fullPath) && !$override_file_name) {
if(file_exists ($fullPath) && !$override_file_name && !$chunks) {
$ext_1 = $ext ? '.'.$ext : '';
$fullPath = $path . '/' . basename($_REQUEST['fullpath'], $ext_1) .'_'. date('ymdHis'). $ext_1;
}
if (!is_dir($folder)) {
$old = umask(0);
mkdir($folder, 0777, true);
umask($old);
}
if (empty($f['file']['error']) && !empty($tmp_name) && $tmp_name != 'none' && $isFileAllowed) {
if (move_uploaded_file($tmp_name, $fullPath)) {
if ($chunkTotal){
$out = @fopen("{$fullPath}.part", $chunkIndex == 0 ? "wb" : "ab");
if ($out) {
$in = @fopen($tmp_name, "rb");
if ($in) {
while ($buff = fread($in, 4096)) { fwrite($out, $buff); }
} else {
$response = array (
'status' => 'error',
'info' => "failed to open output stream"
);
}
@fclose($in);
@fclose($out);
@unlink($tmp_name);
$response = array (
'status' => 'success',
'info' => "file upload successful",
'fullPath' => $fullPath
);
} else {
$response = array (
'status' => 'error',
'info' => "failed to open output stream"
);
}
if ($chunkIndex == $chunkTotal - 1) {
rename("{$fullPath}.part", $fullPath);
}
} else if (move_uploaded_file($tmp_name, $fullPath)) {
// Be sure that the file has been uploaded
if ( file_exists($fullPath) ) {
$response = array (
@ -1181,7 +1221,6 @@ if (isset($_GET['upload']) && !FM_READONLY) {
return '';
}
?>
<link href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/min/dropzone.min.css" rel="stylesheet">
<div class="path">
@ -1225,8 +1264,14 @@ if (isset($_GET['upload']) && !FM_READONLY) {
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.1/min/dropzone.min.js"></script>
<script>
Dropzone.options.fileUploader = {
chunking: true,
chunkSize: 10000000,
forceChunking: true,
retryChunks: true,
retryChunksLimit: 3,
parallelUploads: 1, // does not support more than 1!
timeout: 120000,
maxFilesize: <?php echo MAX_UPLOAD_SIZE; ?>,
maxFilesize: 10000000000,
acceptedFiles : "<?php echo getUploadExt() ?>",
init: function () {
this.on("sending", function (file, xhr, formData) {
@ -1237,9 +1282,11 @@ if (isset($_GET['upload']) && !FM_READONLY) {
});
}).on("success", function (res) {
let _response = JSON.parse(res.xhr.response);
if(_response.status == "error") {
toast(_response.info);
}
}).on("error", function(file, response) {
toast(response);
});