diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index 0d4018b5..41750713 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -184,20 +184,34 @@ public function uploadData(string $data, string $path, string $contentType, int private function joinChunks(string $path, int $chunks) { $tmp = \dirname($path).DIRECTORY_SEPARATOR.'tmp_'.\basename($path).DIRECTORY_SEPARATOR.\basename($path).'_chunks.log'; + + $dest = \fopen($path, 'wb'); + if ($dest === false) { + throw new Exception('Failed to open destination file '.$path); + } + for ($i = 1; $i <= $chunks; $i++) { $part = dirname($tmp).DIRECTORY_SEPARATOR.pathinfo($path, PATHINFO_FILENAME).'.part.'.$i; - $data = file_get_contents($part); - if (! $data) { + $src = \fopen($part, 'rb'); + if ($src === false) { + \fclose($dest); throw new Exception('Failed to read chunk '.$part); } - if (! file_put_contents($path, $data, FILE_APPEND)) { + if (\stream_copy_to_stream($src, $dest) === false) { + \fclose($src); + \fclose($dest); throw new Exception('Failed to append chunk '.$part); } + \fclose($src); \unlink($part); } + + \fclose($dest); \unlink($tmp); - \rmdir(dirname($tmp)); + if (! \rmdir(\dirname($tmp))) { + \trigger_error('Failed to remove temporary chunk directory '.dirname($tmp).' (chunk log: '.$tmp.')', E_USER_WARNING); + } } /**