From 28b74c9c57541b6972716bd8aae7843468886d5d Mon Sep 17 00:00:00 2001 From: "Hermann D. Schimpf" Date: Wed, 15 Jul 2020 17:50:49 -0400 Subject: [PATCH] Creating temp copy using fread() + fwrite() to avoid memory exhaust on large files --- src/JwplatformAPI.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/JwplatformAPI.php b/src/JwplatformAPI.php index c23ca31..8db535a 100644 --- a/src/JwplatformAPI.php +++ b/src/JwplatformAPI.php @@ -238,7 +238,19 @@ public function upload($file_path, $upload_link = [], $api_format = "php") { $post_data = array("file" => new \CURLFile($file_path)); } else { $temp = tmpfile(); - fwrite($temp, file_get_contents($file_path)); + // open original file + $file_in = fopen($file_path, "rb"); + //If we failed to get a file handle, throw an Exception. + if ($file_in === false) throw new Exception('Could not get file handle for: ' . $fileName); + // read until EOF + while (!feof($file_in)) { + // read bytes + $bytes = fread($file_in, 8192); + // write to temp file + fwrite($temp, $bytes); + } + // close original file handler + fclose($file_in); fseek($temp, 0); $localFilePath = stream_get_meta_data($temp)['uri']; $post_data = array("file" => new \CURLFile($localFilePath));