From c3a2f1d6044cbbd1edc51daf35fca97ea5bd45e9 Mon Sep 17 00:00:00 2001 From: Daniele Paganelli Date: Sat, 12 Feb 2022 08:46:07 +0100 Subject: [PATCH] Chunked file upload (#714) Removes any PHP or server-side file-upload limits by using file chunks --- tinyfilemanager.php | 61 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/tinyfilemanager.php b/tinyfilemanager.php index 6cca6c1..cb754fa 100644 --- a/tinyfilemanager.php +++ b/tinyfilemanager.php @@ -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 ''; } ?> -
@@ -1225,8 +1264,14 @@ if (isset($_GET['upload']) && !FM_READONLY) {