Prakhar Prakhar - 4 months ago 66
PHP Question

Upload file with PHP API

I am trying to upload a file to using Box API. According to the docs, the curl request has to look like this:

curl \
-H "Authorization: Bearer ACCESS_TOKEN" -X POST \
-F attributes='{"name":nameOftheFile, "parent":{"id":parentId}}' \
-F file=@file

Here's what I did:

$token = "......";
$url =;

foreach ($_FILES['file']['name'] as $position => $file) {
$file_upload = $_FILES['file']['tmp_name'][$position];
$json = json_encode(array('name' => $file ,array('parent' => array('id' => 0))));
$attrs = array('attributes' => $json,'file'=>'@'.$file_upload);


// Post function
function post($url,$fields){
try {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '.$this->token
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
self::$response = curl_exec($ch);

} catch (Exception $e) {
self::$response = $e->getMessage();

return self::$response;

But it doesn't work. Is there anything wrong in curl part?


Using CurlFile instead of '@path' fixes the issue!