From 067a29f7e5fe1941d870819658952be6d4a1f9b4 Mon Sep 17 00:00:00 2001 From: Mike Russell Date: Fri, 14 Jul 2017 14:45:46 -0400 Subject: [PATCH] Better File Upload Support --- src/Request/AbstractRequest.php | 52 ++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/src/Request/AbstractRequest.php b/src/Request/AbstractRequest.php index 54501b8..3ee8b21 100644 --- a/src/Request/AbstractRequest.php +++ b/src/Request/AbstractRequest.php @@ -285,32 +285,45 @@ public function setBody($body) /** * @inheritdoc */ - public function addFile($bodyKey,$fullFilePath){ + public function addFile($bodyKey,$fullFilePath,$mimeType='',$uploadName=''){ if (file_exists($fullFilePath) && is_readable($fullFilePath)){ - $File = $this->getLegacyFileHandle($fullFilePath); + $File = $this->getLegacyFileHandle($fullFilePath,$mimeType,$uploadName); if (version_compare(PHP_VERSION, '5.5.0') >= 0){ - $File = $this->getFileHandle($fullFilePath); + $File = $this->getFileHandle($fullFilePath,$mimeType,$uploadName); } $this->body[$bodyKey] = $File; - $this->upload = TRUE; + $this->setUpload(TRUE); } return $this; } /** * @param $fullFilePath + * @param $mimeType + * @param $uploadName * @return string */ - private function getLegacyFileHandle($fullFilePath){ - return '@'.$fullFilePath; + private function getLegacyFileHandle($fullFilePath,$mimeType='',$uploadName='') + { + $fileHandle = '@'.$fullFilePath; + if ($mimeType !== ''){ + $fileHandle .= ';type='.$mimeType; + } + if ($uploadName !== ''){ + $fileHandle .= ';filename='.$uploadName; + } + return $fileHandle; } /** * @param $fullFilePath + * @param $mimeType + * @param $uploadName * @return \CURLFile */ - private function getFileHandle($fullFilePath){ - return new \CURLFile($fullFilePath); + private function getFileHandle($fullFilePath,$mimeType='',$uploadName='') + { + return new \CURLFile($fullFilePath,$mimeType,$uploadName); } /** @@ -399,6 +412,24 @@ public function send() return $this; } + /** + * Set the upload property which allows us to track when Body is uploading a file + * @param bool $upload + * @return $this + */ + public function setUpload($upload){ + $this->upload = (bool)$upload; + return $this; + } + + /** + * Check the upload property on the Request Object + * @return bool + */ + public function getUpload(){ + return $this->upload; + } + /** * @inheritdoc */ @@ -480,9 +511,10 @@ protected function configureHeaders(array $headers){ * @return bool */ protected function configureBody($body){ + $upload = $this->getUpload(); switch ($this->method) { case self::HTTP_GET: - if (!$this->upload){ + if (!$upload){ if (is_array($body) || is_object($body)){ $queryParams = http_build_query($body); } else { @@ -499,7 +531,7 @@ protected function configureBody($body){ break; } default: - if ($this->upload){ + if ($upload){ $this->addHeader('Content-Type','multipart/form-data'); } return $this->addCurlOption(CURLOPT_POSTFIELDS, $body);