Skip to content

Instantly share code, notes, and snippets.

Created June 23, 2013 21:38
  • Star 4 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Plupload Examples: Chunking
if (empty($_FILES) || $_FILES['file']['error']) {
die('{"OK": 0, "info": "Failed to move uploaded file."}');
$chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
$chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
$fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : $_FILES["file"]["name"];
$filePath = "uploads/$fileName";
// Open temp file
$out = @fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");
if ($out) {
// Read binary input stream and append it to temp file
$in = @fopen($_FILES['file']['tmp_name'], "rb");
if ($in) {
while ($buff = fread($in, 4096))
fwrite($out, $buff);
} else
die('{"OK": 0, "info": "Failed to open input stream."}');
} else
die('{"OK": 0, "info": "Failed to open output stream."}');
// Check if file has been uploaded
if (!$chunks || $chunk == $chunks - 1) {
// Strip the temp .part suffix off
rename("{$filePath}.part", $filePath);
die('{"OK": 1, "info": "Upload successful."}');
Copy link

rammanoj commented Aug 30, 2018

Thanks for the code. But how to handle partially uploaded files? ( I mean the files are uploaded partially and internet connection got interrupted and upload got stopped ).

Copy link

veekthoven commented Dec 16, 2018

@rammanoj i think, setting the "max_retries" property to a value greater than 0 will retry to continue the file upload for x times, given that x is the value of "max_retries" you provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment