mirror of
https://github.com/retailcrm/mailgun-php.git
synced 2024-11-23 21:16:02 +03:00
Explicit creation and deletion of a temporary file
This commit is contained in:
parent
b27bab7b9f
commit
230b2ff67b
@ -134,12 +134,15 @@ class Message extends HttpApi
|
|||||||
private function prepareFile(string $fieldName, array $filePath): array
|
private function prepareFile(string $fieldName, array $filePath): array
|
||||||
{
|
{
|
||||||
$filename = isset($filePath['filename']) ? $filePath['filename'] : null;
|
$filename = isset($filePath['filename']) ? $filePath['filename'] : null;
|
||||||
|
$deleteRequired = false;
|
||||||
|
|
||||||
if (isset($filePath['fileContent'])) {
|
if (isset($filePath['fileContent'])) {
|
||||||
// File from memory
|
// File from memory
|
||||||
$resource = fopen('php://temp', 'r+');
|
$filename = tempnam(sys_get_temp_dir(), "MAILGUN_TMP");
|
||||||
|
$resource = fopen($filename, 'r+');
|
||||||
fwrite($resource, $filePath['fileContent']);
|
fwrite($resource, $filePath['fileContent']);
|
||||||
rewind($resource);
|
rewind($resource);
|
||||||
|
$deleteRequired = true;
|
||||||
} elseif (isset($filePath['filePath'])) {
|
} elseif (isset($filePath['filePath'])) {
|
||||||
// File form path
|
// File form path
|
||||||
$path = $filePath['filePath'];
|
$path = $filePath['filePath'];
|
||||||
@ -158,6 +161,7 @@ class Message extends HttpApi
|
|||||||
'name' => $fieldName,
|
'name' => $fieldName,
|
||||||
'content' => $resource,
|
'content' => $resource,
|
||||||
'filename' => $filename,
|
'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'])) {
|
if (is_array($param) && array_key_exists('content', $param) && is_resource($param['content'])) {
|
||||||
fclose($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']);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user