Skip to content

Commit

Permalink
try prevent remaining temporary file during export study data
Browse files Browse the repository at this point in the history
  • Loading branch information
salimkanoun committed Jul 17, 2024
1 parent 2ee52a3 commit 9ab7697
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions GaelO2/app/GaelO/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit 9ab7697

Please sign in to comment.