Skip to content

Commit

Permalink
Use CURLFile for upload, when available
Browse files Browse the repository at this point in the history
  • Loading branch information
ilijastuden committed Feb 10, 2018
1 parent 02b58cb commit 41d1e9d
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace ActiveCollab\SDK;

use ActiveCollab\SDK\Exceptions\CallFailed;
use CURLFile;

/**
* Connector makes requests and returns API responses.
Expand Down Expand Up @@ -86,6 +87,7 @@ public function post($url, $headers = null, $post_data = null, $files = null)
}

$counter = 1;
$safe_file_upload_turned_off = false;

foreach ($files as $file) {
if (is_array($file)) {
Expand All @@ -95,7 +97,17 @@ public function post($url, $headers = null, $post_data = null, $files = null)
$mime_type = 'application/octet-stream';
}

$post_data['attachment_'.$counter++] = '@'.$path.';type='.$mime_type;
if ((version_compare(PHP_VERSION, '5.5') >= 0)) {
$post_data['attachment_' . $counter++] = new CURLFile($path);
curl_setopt($http, CURLOPT_SAFE_UPLOAD, true);
} else {
$post_data['attachment_' . $counter++] = '@' . $path . ';type=' . $mime_type;

if (!$safe_file_upload_turned_off) {
curl_setopt($http, CURLOPT_SAFE_UPLOAD, false);
$safe_file_upload_turned_off = true;
}
}
}

curl_setopt($http, CURLOPT_SAFE_UPLOAD, false); // PHP 5.6 compatibility for file uploads
Expand Down

0 comments on commit 41d1e9d

Please sign in to comment.