From cf18810abc6cfbdc9c6c1899a36b3f29e5888d6a Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 23 Feb 2026 02:23:44 +0000 Subject: [PATCH 1/2] fix: enhance joinChunks method to improve chunk file handling --- src/Storage/Device/Local.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index 0d4018b5..1e19576b 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -184,18 +184,30 @@ 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, 'ab'); + 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)); } From 343d0638270363215ab14f3737727f6560c763cc Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 23 Feb 2026 02:35:32 +0000 Subject: [PATCH 2/2] fix: update joinChunks method to open destination file in write mode and handle temporary directory removal errors --- src/Storage/Device/Local.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index 1e19576b..41750713 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -185,7 +185,7 @@ private function joinChunks(string $path, int $chunks) { $tmp = \dirname($path).DIRECTORY_SEPARATOR.'tmp_'.\basename($path).DIRECTORY_SEPARATOR.\basename($path).'_chunks.log'; - $dest = \fopen($path, 'ab'); + $dest = \fopen($path, 'wb'); if ($dest === false) { throw new Exception('Failed to open destination file '.$path); } @@ -209,7 +209,9 @@ private function joinChunks(string $path, int $chunks) \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); + } } /**