# CMS Files API on PHP - Upload a new file - Error 400 Bad Request

**URL:** <https://community.hubspot.com/t/cms-files-api-on-php-upload-a-new-file-error-400-bad-request/27190>\
**Category:** APIs & Integrations\
**Tags:** cms-content, developer-tools\
**Created:** [October 26, 2020, 5:17am UTC](https://community.hubspot.com/t/cms-files-api-on-php-upload-a-new-file-error-400-bad-request/27190 "2020-10-26T05:17:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![melumbs](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/melumbs/32/77060_2.png) [@melumbs](https://community.hubspot.com/u/melumbs)\
**Post date:** [October 26, 2020, 5:17am UTC](https://community.hubspot.com/t/cms-files-api-on-php-upload-a-new-file-error-400-bad-request/27190/1 "2020-10-26T05:17:07Z")

</div>

Hi, I’m currently integrating CMS Files API to my project to be able to upload files from my project to the HubSpot File Manager. I followed and used the sample PHP script on [https://legacydocs.hubspot.com/docs/methods/files/v3/upload\_new\_file](https://legacydocs.hubspot.com/docs/methods/files/v3/upload_new_file) but I kept getting Error 400 - Bad Request response from the server. I’m using Oauth2.0 for authentication.

```lia-code-sample
function upload_file($file_attachment = null) {
 if (!$file_attachment || !$this->access_token) return;

 $this->url = 'http://api.hubapi.com/filemanager/api/v3/files/upload';
 $this->headers[] = 'content-type: multipart/form-data';
 $this->headers[] = sprintf('Authorization: Bearer %s', $this->access_token);

 $upload_file = new CURLFile($file_attachment['file_attachment']['tmp_name'], $file_attachment['file_attachment']['type'], $file_attachment['file_attachment']['name']);

 $file_options = array(
 "access" => "PUBLIC_INDEXABLE",
 "ttl" => "P3M",
 "overwrite" => false,
 "duplicateValidationStrategy" => "NONE",
 "duplicateValidationScope" => "ENTIRE_PORTAL"
 );

 $post_data = array(
 "file" => $upload_file,
 "fileName" => $file_attachment->title,
 "options" => json_encode($file_options),
 "folderPath" => "/docs",
 );

 $process = $this->process($post_data, 'POST', TRUE);

 return $process;
 }

function process($data = null, $action = 'POST', $return_response = FALSE) {
 $ch = curl_init();

 curl_setopt($ch, CURLOPT_URL, $this->url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

 if ($action !== 'GET') {
 curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
 curl_setopt($ch, CURLOPT_POST, 1);
 }

 if($action == 'DELETE') curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");

 if (count($this->headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);

 $result = curl_exec($ch);
 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 if (curl_errno($ch)) {
 echo 'Error:' . curl_error($ch);
 }
 curl_close($ch);

 if ($return_response) return $result;

 if ($http_code == 200) return TRUE;
 return;
 }

```

I also checked other posts like [https://community.hubspot.com/t5/APIs-Integrations/Not-able-to-upload-file-through-file-API/td-p/301404](https://community.hubspot.com/t5/APIs-Integrations/Not-able-to-upload-file-through-file-API/td-p/301404) - which uses the old v2 file upload endpoint, tried applying it to my code, but still returning the same error.

I tested other endpoints like Delete a File, Create a Folder and it works properly. Upload a file is the only endpoint that returns Error 400. Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [October 27, 2020, 5:10pm UTC](https://community.hubspot.com/t/cms-files-api-on-php-upload-a-new-file-error-400-bad-request/27190/2 "2020-10-27T17:10:43Z")

</div>

Hello @melumbs ,

Sorry that you are having these issues. I just ran the example code from the docs and successfully uploaded a file.

Wondering if [@quentin\_lamamy](https://community.hubspot.com/t5/user/viewprofilepage/user-id/148828) has any thoughts on this one!

---

<div class="post-metadata">

**Author:** ![quentin\_lamamy](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/quentin_lamamy/32/20212_2.png) [@quentin\_lamamy](https://community.hubspot.com/u/quentin_lamamy)\
**Post date:** [October 27, 2020, 5:32pm UTC](https://community.hubspot.com/t/cms-files-api-on-php-upload-a-new-file-error-400-bad-request/27190/3 "2020-10-27T17:32:05Z")

</div>

I will check this post in 20 min I’m on the road to go back home 🙂

---

<div class="post-metadata">

**Author:** ![melumbs](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/melumbs/32/77060_2.png) [@melumbs](https://community.hubspot.com/u/melumbs)\
**Post date:** [October 28, 2020, 5:06am UTC](https://community.hubspot.com/t/cms-files-api-on-php-upload-a-new-file-error-400-bad-request/27190/4 "2020-10-28T05:06:38Z")

</div>

Hi @dennisedson, can you confirm if you run the code on an HTTPS environment? Because I’ve been testing the same PHP code from the example on localhost and kept getting error 400.

---

<div class="post-metadata">

**Author:** ![quentin\_lamamy](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/quentin_lamamy/32/20212_2.png) [@quentin\_lamamy](https://community.hubspot.com/u/quentin_lamamy)\
**Post date:** [October 27, 2020, 6:14pm UTC](https://community.hubspot.com/t/cms-files-api-on-php-upload-a-new-file-error-400-bad-request/27190/5 "2020-10-27T18:14:46Z")

</div>

Hi @melumbs,

Can you provide us the request and the answer in the call-log of your api key ? You can get it in settings \> integrations \> apikey. It will help us to understand what is going wrong.

---

<div class="post-metadata">

**Author:** ![melumbs](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/melumbs/32/77060_2.png) [@melumbs](https://community.hubspot.com/u/melumbs)\
**Post date:** [October 28, 2020, 5:08am UTC](https://community.hubspot.com/t/cms-files-api-on-php-upload-a-new-file-error-400-bad-request/27190/6 "2020-10-28T05:08:26Z")

</div>

Hi [@quentin\_lamamy](https://community.hubspot.com/t5/user/viewprofilepage/user-id/148828) ,

I have checked the call logs from the two HubSpot accounts that I’m using for the development of the integration. Unfortunately, there are no API logs under the file upload endpoint. Not sure if it’s because I’m using Oauth2.0 for the authentication.

---

<div class="post-metadata">

**Author:** ![quentin\_lamamy](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/quentin_lamamy/32/20212_2.png) [@quentin\_lamamy](https://community.hubspot.com/u/quentin_lamamy)\
**Post date:** [October 28, 2020, 7:38am UTC](https://community.hubspot.com/t/cms-files-api-on-php-upload-a-new-file-error-400-bad-request/27190/7 "2020-10-28T07:38:02Z")

</div>

ok, i see that your code is an extract of a class, can you post the whol class code and the code which is using this class to upload a file ?

---

<div class="post-metadata">

**Author:** ![melumbs](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/melumbs/32/77060_2.png) [@melumbs](https://community.hubspot.com/u/melumbs)\
**Post date:** [October 28, 2020, 8:18am UTC](https://community.hubspot.com/t/cms-files-api-on-php-upload-a-new-file-error-400-bad-request/27190/8 "2020-10-28T08:18:07Z")

</div>

Hi [@quentin\_lamamy](https://community.hubspot.com/t5/user/viewprofilepage/user-id/148828),

Thanks but I think I finally got it working now. I used the code below as a reference and convert it into a PHP class.

```lia-code-sample
$apikey = '[Your HubSpot API Key]';
$url = "http://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=" . $apikey;
$realpath = getcwd()."\\example_file.txt";

// cURL headers
$headers = array("Content-Type:multipart/form-data"); 

$upload_file = new CURLFile($realpath,'application/octet-stream', 'example_file.txt');

$file_options = array(
 "access" => "PUBLIC_INDEXABLE",
 "ttl" => "P3M",
 "overwrite" => false,
 "duplicateValidationStrategy" => "NONE",
 "duplicateValidationScope" => "ENTIRE_PORTAL"
);

$post_data = array(
 "file" => $upload_file,
 "options" => json_encode($file_options),
 "folderPath" => "/", // /docs
);

$ch = curl_init(); 

$options = array( 
 CURLOPT_URL => $url, 
 CURLOPT_HEADER => true, 
 CURLOPT_POST => 1, 
 CURLOPT_HTTPHEADER => $headers, 
 CURLOPT_POSTFIELDS => $post_data, 
 CURLOPT_RETURNTRANSFER => true,
); 

// cURL options 
curl_setopt_array($ch, $options); 
$result = curl_exec($ch); 

print_r($result);

if(!curl_errno($ch)) { 
 $info = curl_getinfo($ch); 
 if ($info['http_code'] == 200) {
 $msg = "File uploaded successfully";
 echo $msg; 
 }
} else { 
 $errmsg = curl_error($ch); 
 echo $errmsg; 
} 

curl_close($ch);

```

The code above also works well when using OAuth 2.0 for authentication.

It’s weird because it doesn’t work (for me at least) when I use

curl\_setopt()

for setting the curl options.

Using

curl\_setopt\_array()

for setting up the curl options did the trick for me.

---

<div class="post-metadata">

**Author:** ![quentin\_lamamy](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/quentin_lamamy/32/20212_2.png) [@quentin\_lamamy](https://community.hubspot.com/u/quentin_lamamy)\
**Post date:** [October 28, 2020, 8:20am UTC](https://community.hubspot.com/t/cms-files-api-on-php-upload-a-new-file-error-400-bad-request/27190/9 "2020-10-28T08:20:22Z")

</div>

Good news if you succeed to do what you want. If your issue is solved don’t forget to flag your issue as solved.

---

<div class="post-metadata">

**Author:** ![melumbs](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/melumbs/32/77060_2.png) [@melumbs](https://community.hubspot.com/u/melumbs)\
**Post date:** [October 28, 2020, 9:03am UTC](https://community.hubspot.com/t/cms-files-api-on-php-upload-a-new-file-error-400-bad-request/27190/10 "2020-10-28T09:03:23Z")

</div>

Reposting the Solution:

Got it working using the code below - converted it to a PHP class afterwards:

```lia-code-sample
$apikey = '[Your HubSpot API Key]';
$url = "http://api.hubapi.com/filemanager/api/v3/files/upload?hapikey=" . $apikey;
$realpath = getcwd()."\\example_file.txt";

// cURL headers
$headers = array("Content-Type:multipart/form-data"); 

$upload_file = new CURLFile($realpath,'application/octet-stream', 'example_file.txt');

$file_options = array(
 "access" => "PUBLIC_INDEXABLE",
 // "ttl" => "P3M",
 "overwrite" => false,
 "duplicateValidationStrategy" => "NONE",
 "duplicateValidationScope" => "ENTIRE_PORTAL"
);

$post_data = array(
 "file" => $upload_file,
 "options" => json_encode($file_options),
 "folderPath" => "/",
);

$ch = curl_init(); 

$options = array( 
 CURLOPT_URL => $url, 
 CURLOPT_HEADER => true, 
 CURLOPT_POST => 1, 
 CURLOPT_HTTPHEADER => $headers, 
 CURLOPT_POSTFIELDS => $post_data, 
 CURLOPT_RETURNTRANSFER => true,
); 

// cURL options 
curl_setopt_array($ch, $options); 
$result = curl_exec($ch); 
echo $result; 

if(!curl_errno($ch)) { 
 $info = curl_getinfo($ch); 
 if ($info['http_code'] == 200) {
 $msg = "File uploaded successfully";
 echo $msg; 
 }
} else { 
 $errmsg = curl_error($ch); 
 echo $errmsg; 
} 

curl_close($ch);

```

curl\_setopt()

is not working for me, used

curl\_setopt\_array()

to get it work.
