From 230b2ff67be448d3532cdb7c6904d20298af98f0 Mon Sep 17 00:00:00 2001 From: Vitaliy Chesnokov Date: Tue, 26 Nov 2019 18:29:31 +0300 Subject: [PATCH] Explicit creation and deletion of a temporary file --- src/Api/Message.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Api/Message.php b/src/Api/Message.php index d51553a..5529ad6 100644 --- a/src/Api/Message.php +++ b/src/Api/Message.php @@ -134,12 +134,15 @@ class Message extends HttpApi private function prepareFile(string $fieldName, array $filePath): array { $filename = isset($filePath['filename']) ? $filePath['filename'] : null; + $deleteRequired = false; if (isset($filePath['fileContent'])) { // File from memory - $resource = fopen('php://temp', 'r+'); + $filename = tempnam(sys_get_temp_dir(), "MAILGUN_TMP"); + $resource = fopen($filename, 'r+'); fwrite($resource, $filePath['fileContent']); rewind($resource); + $deleteRequired = true; } elseif (isset($filePath['filePath'])) { // File form path $path = $filePath['filePath']; @@ -158,6 +161,7 @@ class Message extends HttpApi 'name' => $fieldName, 'content' => $resource, 'filename' => $filename, + 'deleteRequired' => $deleteRequired, ]; } @@ -189,6 +193,13 @@ class Message extends HttpApi if (is_array($param) && array_key_exists('content', $param) && is_resource($param['content'])) { fclose($param['content']); } + if (is_array($param)) { + $isFile = array_key_exists('filename', $param) && is_file($param['filename']); + $deleteRequired = $param['deleteRequired'] ?? false; + if ($isFile && $deleteRequired) { + unlink($param['filename']); + } + } } } }