Reputation: 11091
I would like to implement chunked upload for large files. I was following this documentation https://docs.nextcloud.com/server/20/developer_manual/client_apis/WebDAV/chunking.html but I struggle with the final the most important step - Assembling the chunks
The code I used moves the whole temp directory to new directory that is named as the final file.
$sourcePath = $tempFolder . '.file';
$destinationPath = $baseUri . $uploadedFolderName . $uploadedFileName;
// Send the MOVE request
$response = $client->request('MOVE', $tempFolder, null, [
'Destination' => $destinationPath
]);
My thinking is that
Could someone please help me to fix this?
The code for uploading chunks
// Read the file in chunks and upload
$fp = fopen($filePath, 'rb');
$chunkNumber = 0;
$startByte = 0;
while (!feof($fp)) {
$chunk = fread($fp, $chunkSize);
$endByte = $startByte + strlen($chunk) - 1;
// Generate a unique filename based on start and end bytes
$chunkFileName = sprintf('%020d-%020d', $startByte, $endByte);
$chunkPath = $tempFolder . '/' . $chunkFileName;
// Upload the chunk
// $client->put($chunkPath, $chunk);
// // Upload the chunk
try {
$client->request('PUT', $chunkPath, $chunk);
echo "Uploaded chunk $chunkNumber successfully.\n";
} catch (Exception $e) {
echo "Error uploading chunk $chunkNumber: " . $e->getMessage() . "\n";
break; // Handle errors appropriately
}
$startByte += strlen($chunk);
$chunkNumber++;
}
fclose($fp);
Upvotes: 0
Views: 35