# File upload using PHP curl post request.

**URL:** <https://community.hubspot.com/t/file-upload-using-php-curl-post-request/16592>\
**Category:** APIs & Integrations\
**Tags:** developer-tools\
**Created:** [October 7, 2019, 12:51pm UTC](https://community.hubspot.com/t/file-upload-using-php-curl-post-request/16592 "2019-10-07T12:51:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kishan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kishan/32/92797_2.png) [@kishan](https://community.hubspot.com/u/kishan)\
**Post date:** [October 7, 2019, 12:51pm UTC](https://community.hubspot.com/t/file-upload-using-php-curl-post-request/16592/1 "2019-10-07T12:51:36Z")

</div>

Hello.

I am using php curl to upload file here i have code.

```auto
$url = "http://api.hubapi.com/filemanager/api/v2/files?hapikey=".$apikey;//change your api key here
 $realpath = '@'.$_FILES["file_select"]["tmp_name"].$_FILES["file_select"]["name"];
 $file_data= array('files[]'=>$realpath);
 $headers = array("Content-Type:multipart/form-data" , "hapikey: ".$apikey); // cURL headers for file uploading
 $postfields = array("files[]"=>$realpath,'file_names'=> $_FILES["file_select"]["name"]);

 $params = "--custom-boundary-123456789\r\n".
 "Content-Disposition: form-data; name=\"api\"\r\n\r\n".
 "static\r\n".
 "--custom-boundary-123456789\r\n".
 "Content-Disposition: form-data; name=\"files[]\"; filename=".$_FILES["file_select"]["name"]."\"\r\n".
 "Content-Type: text/html\r\n\r\n".
 "<div>Content for here here...</div>\r\n".
 "--custom-boundary-123456789\r\n".
 "Content-Disposition: form-data; name=\"files[]\"; filename=".$_FILES["file_select"]["name"]."\"\r\n".
 "Content-Type: text/html\r\n\r\n".
 "<div>Content for file-b.html here...</div>\r\n".
 "--custom-boundary-123456789--";

 $request_headers = array('Content-Type: multipart/form-data; boundary=----custom-boundary-123456789--');

 $ch = curl_init('http://api.hubapi.com/filemanager/api/v2/files?hapikey='.$apikey);
 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
 curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);

 $reply = curl_exec($ch);
 $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 curl_close($ch);

 echo $reply;

```

and

```auto
$url = "http://api.hubapi.com/filemanager/api/v2/files?hapikey=".$apikey;//change your api key here
 $realpath = '@'.$_FILES["file_select"]["tmp_name"].$_FILES["file_select"]["name"];
 $file_data= array('files[]'=>$realpath);
 $headers = array("Content-Type:multipart/form-data" , "hapikey: ".$apikey); // cURL headers for file uploading
 $postfields = array("files[]"=>$realpath,'file_names'=> $_FILES["file_select"]["name"]);

$ch = curl_init();

 @curl_setopt($ch, CURLOPT_POST, true);
 @curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($file_data));
 @curl_setopt($ch, CURLOPT_URL, $url);
 @curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

 @curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 $response = @curl_exec($ch);
 $status_code = @curl_getinfo($ch, CURLINFO_HTTP_CODE);
 $curl_errors = curl_error($ch);
 @curl_close($ch);
 // echo "curl Errors: " . $curl_errors;
 // echo "\nStatus code: " . $status_code;
 // echo "\nResponse: " . $response;

 print_r($response);

```

Both response shows bad request.  
what should i do?

---

<div class="post-metadata">

**Author:** ![Derek\_Gervais](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/derek_gervais/32/17429_2.png) [@Derek\_Gervais](https://community.hubspot.com/u/Derek_Gervais)\
**Post date:** [October 7, 2019, 8:33pm UTC](https://community.hubspot.com/t/file-upload-using-php-curl-post-request/16592/2 "2019-10-07T20:33:32Z")

</div>

Hey @kishan ,

I’m not familiar enough with PHP to troubleshoot the code here; perhaps someone else in the community is more familiar with PHP? A quick search also returned a couple basic resources online; these may or may not be helpful to you:

- [https://stackoverflow.com/questions/17862004/send-file-using-multipart-form-data-request-in-php](https://stackoverflow.com/questions/17862004/send-file-using-multipart-form-data-request-in-php)
- [PHP upload file with curl (multipart/form-data) · GitHub](https://gist.github.com/maxivak/18fcac476a2f4ea02e5f80b303811d5f)

Regardless, can you provide more detail on the error response you’re seeing from HubSpot? Ideally, the status code & error message would be useful.
