This topic is locked

upload large files in small parts

12/3/2024 10:39:56 AM
PHPRunner General questions
A
Andrea Iorio author

Hi, I have a big problem with an application that I'm developing in phprunner.
The application, to make a long story short, provides the possibility of uploading large files, up to 300Mb.
The company server where the application must run, due to security policy, does not allow this.
I saw that a solution would be to divide the files into smaller parts, or chunks, and reassemble them once uploaded. But I don't know how to do it, can you help me?
the files are uploaded via a file/image in the unpload folder. Thanks for your help.

A
Andrea Iorio author 12/3/2024

I found this solution, is it possible to use it in phprunner?

// (A) HELPER FUNCTION - SERVER RESPONSE
function verbose ($ok=1, $info="") {
if ($ok==0) { http_response_code(400); }
exit(json_encode(["ok"=>$ok, "info"=>$info]));
}

// (B) INVALID UPLOAD
if (empty($_FILES) || $_FILES["file"]["error"]) {
verbose(0, "Failed to move uploaded file.");
}

// (C) UPLOAD DESTINATION - CHANGE FOLDER IF REQUIRED!
$filePath = DIR . DIRECTORY_SEPARATOR . "uploads";
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;

// (D) 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"); }

// (E) CHECK IF FILE HAS BEEN UPLOADED
if (!$chunks || $chunk == $chunks - 1) { rename("{$filePath}.part", $filePath); }
verbose(1, "Upload OK");

A
Andrea Iorio author 12/4/2024

Can anyone give me a hand?

Sergey Kornilov 12/4/2024

I don't think this is going to work. I don't know what this code does but this is PHP code so it handles the receiving part. I don't know how do you tell the web browser to upload the large file in parts.

I think the most reasonable solution is to create a subdomain on your website where this application can be deployed and set different upload size limit for this subdomain only. This is what we do on our own web site when we upload DevClub videos.

A
Andrea Iorio author 12/6/2024

Maybe I succeeded, so I'll explain how to do it here. But I need some important information. I have an upload form with a field called "allegato" set as file/image. I need to intercept the change in the "Allegato" field of the original PHPRunner file and put it in this javascript command "$("input[name='value_allegato' ]").on("change", function () {
file = this.file[0]; // Get the file from the field.
This way I can split the file into chumks, load the chumks and rebuild it after loading all the chumks of the file.

Can you help me?