From 9ab76974890c6b87ad4833f92a3abd285d1b37d9 Mon Sep 17 00:00:00 2001 From: Salim Kanoun Date: Wed, 17 Jul 2024 23:22:23 +0200 Subject: [PATCH] try prevent remaining temporary file during export study data --- GaelO2/app/GaelO/Util.php | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/GaelO2/app/GaelO/Util.php b/GaelO2/app/GaelO/Util.php index fad808ffc..8353492f3 100644 --- a/GaelO2/app/GaelO/Util.php +++ b/GaelO2/app/GaelO/Util.php @@ -47,22 +47,24 @@ public static function camelCaseToSnakeCase(string $string, string $us = "_"): s public static function addStoredFilesInZipAndClose(ZipArchive $zip, ?string $path) { - - $files = FrameworkAdapter::getStoredFiles($path); - $temporaryFilesToDelete=[]; - foreach ($files as $file) { - // Add current file to archive using data as stream to prevent running out memory for large files - $tempraryFilePath = tempnam(ini_get('upload_tmp_dir'), 'TMPEXP_'); - $temporaryFilesToDelete[] = $tempraryFilePath; - $fileContent = FrameworkAdapter::getFile($file, true); - stream_copy_to_stream($fileContent, fopen($tempraryFilePath, 'w')); - $zip->addFile($tempraryFilePath, $file); - } - //Close to build the zip as the operation is async - $zip->close(); - //Delete all temporary files - foreach($temporaryFilesToDelete as $temp){ - unlink($temp); + $temporaryFilesToDelete = []; + try { + $files = FrameworkAdapter::getStoredFiles($path); + foreach ($files as $file) { + // Add current file to archive using data as stream to prevent running out memory for large files + $tempraryFilePath = tempnam(ini_get('upload_tmp_dir'), 'TMPEXP_'); + $temporaryFilesToDelete[] = $tempraryFilePath; + $fileContent = FrameworkAdapter::getFile($file, true); + stream_copy_to_stream($fileContent, fopen($tempraryFilePath, 'w')); + $zip->addFile($tempraryFilePath, $file); + } + //Close to build the zip as the operation is async + $zip->close(); + } finally { + //Delete all temporary files + foreach ($temporaryFilesToDelete as $temp) { + unlink($temp); + } } }