Level 75
Here is one answer: https://stackoverflow.com/questions/36045690/merging-file-chunks-in-php
And this gist: https://gist.github.com/unique1984/d8a4c4ace2dece7b8a75e02357e5f2bd
How do I merge the file chunks uploaded by filepond.
https://pqina.nl/filepond/docs/patterns/api/server/
This is what I tried Process is where I initialize the temp folder
public function process(Request $request)
{
$folder = uniqid() . '-' . now()->timestamp;
TemporaryFile::create([
'folder' => $folder,
'filename' => '',
'original_filename' => '',
'total_file_size' => $request->header('Upload-Length')
]);
return response($folder, 200, [
'Content-Type' => 'text/plain'
]);
}
Patch is where I send the chunks
public function patch(Request $request)
{
$prefixPath = 'videos/tmp/';
$folder = $request->patch;
$filename = '/part' . now()->timezone . str_pad($request->header('Upload-Offset'), 20, '0', STR_PAD_LEFT) . '.part';
Storage::disk('local')
->put(
$prefixPath . $folder . $filename,
$request->getContent()
);
return true;
}
Final Merge
public function merge(string $folder)
{
$directory = 'videos/tmp/' . $folder;
$files = Storage::files($directory);
$files = Arr::sort($files);
$firstChunk = Arr::first($files);
$remainderChunks = Arr::except($files, 0);
// dd($firstChunk, $remainderChunks);
$wholeFile = $directory . '/FinalVideo.mp4';
Storage::disk('local')
->put(
$wholeFile,
Storage::disk('local')->get($firstChunk)
);
foreach ($remainderChunks as $key => $chunk) {
Storage::disk('local')->append($wholeFile, Storage::disk('local')->get($chunk));
}
}
Please or to participate in this conversation.