cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Are you new to Dropbox? Our Newcomers group is the perfect place to learn the basics, find out more here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: uploadToDropbox cURL Root

uploadToDropbox cURL Root

MeloX
Explorer | Level 3
Go to solution

Hi,

 

I was trying cURL, but I am pretty bad at it.

 

I would need your assistance.

 

function uploadToDropbox($path, $folder="\0", $name="") {

$url = "https://api.dropbox.com/oauth2/token";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, "xxx");

$headers = array(
"Content-Type: application/x-www-form-urlencoded",
"Authorization: Basic xx",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$data = "grant_type=refresh_token&refresh_token=xx";

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);


$resp = curl_exec($curl);
curl_close($curl);
$outCurl = json_decode($resp,True);
$token = $outCurl["access_token"];

$dropboxFp = fopen($path, 'rb');
$dropboxSize = filesize($path);
$dropboxCheaders = array('Authorization: Bearer '.$token,
'Content-Type: application/octet-stream',
'Dropbox-API-Path-Root: {".tag": "namespace_id", "namespace_id": "7"}',
'Dropbox-API-Arg: {"path":"/Outside of Me Folder/Folder/Folder2/Folder3/Folder4/'.$_SESSION['company_name'].'/'.$_SESSION['product_name'].'/'.$folder.$name.'", "mode":"add"}');

$dropboxCh = curl_init('https://content.dropboxapi.com/2/files/upload');
curl_setopt($dropboxCh, CURLOPT_HTTPHEADER, $dropboxCheaders);
curl_setopt($dropboxCh, CURLOPT_PUT, true);
curl_setopt($dropboxCh, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($dropboxCh, CURLOPT_INFILE, $dropboxFp);
curl_setopt($dropboxCh, CURLOPT_INFILESIZE, $dropboxSize);
curl_setopt($dropboxCh, CURLOPT_RETURNTRANSFER, true);
$dropboxResponse = curl_exec($dropboxCh);

echo $dropboxResponse;
curl_close($dropboxCh);
fclose($dropboxFp);
}

$prodreport = "Reports/";
$prodimgs = "Product Images/";
$prodothrs = "Others/";

 

It put things in this folder:

Me > Folder > Folder2 > Folder3 > Folder4 > Folder5 > test

 

It should put things here:

Outside of Me Folder > Folder > Folder2 > Folder3 > Folder4 > Folder5 > test

 

 

Thank you!

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

By default, API calls operate in the "member folder" of the connected account, not the "team space". That is why you would get files uploaded to your own member folder by default. From your description, you want to upload them to the team space instead.

 

You can configure API calls to operate in the "team space" instead. To do so, you'll need to set the "Dropbox-Api-Path-Root" header. In the code you shared here, I see you do have the "Dropbox-Api-Path-Root" header, but you're setting a "namespace_id" of "7". The "7" is just an example from the guide/documentation. For your actual code, you will need to set your own value. Refer to the Team Files Guide and Path Root Header Modes documentation for information on how to retrieve and use the correct value for the connected account.

View solution in original post

2 Replies 2

Greg-DB
Dropbox Staff
Go to solution

By default, API calls operate in the "member folder" of the connected account, not the "team space". That is why you would get files uploaded to your own member folder by default. From your description, you want to upload them to the team space instead.

 

You can configure API calls to operate in the "team space" instead. To do so, you'll need to set the "Dropbox-Api-Path-Root" header. In the code you shared here, I see you do have the "Dropbox-Api-Path-Root" header, but you're setting a "namespace_id" of "7". The "7" is just an example from the guide/documentation. For your actual code, you will need to set your own value. Refer to the Team Files Guide and Path Root Header Modes documentation for information on how to retrieve and use the correct value for the connected account.

MeloX
Explorer | Level 3
Go to solution

Thank you so much, Greg!

 

I got it to work!

 

I appreciate your help.

Need more support?