Skip to content

fix the file attachment for curl #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion src/Jira/Api/Client/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ public function sendRequest($method, $url, $data = array(), $endpoint, Authentic
if ($method == "POST") {
curl_setopt($curl, CURLOPT_POST, 1);
if ($isFile) {
$file = $data['file'];
$file = ltrim($file, '@');
$fileData = explode('/', $file);
$postName = array_pop($fileData);
$contentType = file_exists($file) ? mime_content_type($file) : null;
$data['file'] = $this->getCurValueForFile($file, $contentType, $postName);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
Expand Down Expand Up @@ -113,5 +119,29 @@ public function sendRequest($method, $url, $data = array(), $endpoint, Authentic
return $data;
}

/**
* Creates a valid file for curl request.
*
* @param string $fileName
* @param string|null $contentType
* @param string|null $postName
*
* @return \CURLFile|string
*/
private function getCurValueForFile($fileName, $contentType = null, $postName = null)
{
// PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
// See: https://wiki.php.net/rfc/curl-file-upload
if (function_exists('curl_file_create')) {
return curl_file_create($fileName, $contentType, $postName);
}

// Use the old style if using an older version of PHP
$value = "@{filename};filename=" . $postName;
if ($contentType) {
$value .= ';type=' . $contentType;
}

}
return $value;
}
}