Start 2025 on time and up to date. Seamlessly integrate your calendars into Dropbox with these simple steps.

Forum Discussion

FSUInnovation's avatar
FSUInnovation
Explorer | Level 4
5 years ago

http header issue with file send via php curl

I keep getting errors in my php code regarding a missing authorization header in my http request using curl. This is what my code looks like:

$doc = $_FILES['Resume'];
$filename = $doc['tmp_name'];
$dropbox_token = "token";
$dropbox_url = "https://content.dropboxapi.com/2/files/upload";
/*$dropbox_api_headers = array('Authorization: Bearer '. $dropbox_token,
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: '.
json_encode(
array(
"path"=> '/'. basename($filename),
"mode" => "add",
"autorename" => true,
"mute" => false
)
)

);*/
$opendoc = fopen($filename, 'rb');
$docsize = $_FILES['Resume']['size'];
$d1curl = curl_init();
$timeout = 50;
curl_setopt($d1curl, CURLOPT_URL, $dropbox_url);
curl_setopt($d1curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($d1curl, CURLOPT_HEADER, [
utf8_encode('Authorization: Bearer ' . $dropbox_token),
utf8_encode('Content-Type: application/octet-stream'),
utf8_encode('Dropbox-API-Arg: '.
json_encode(
array(
"path"=> '/'. basename($filename),
"mode" => "add",
"autorename" => true,
"mute" => false
)))]);
curl_setopt($d1curl, CURLOPT_POST, true);
curl_setopt($d1curl, CURLOPT_POSTFIELDS, fread($opendoc, $docsize));
curl_setopt($d1curl, CURLOPT_RETURNTRANSFER, true);
$dropbox_upload = curl_exec($d1curl);
$http_request = curl_getinfo($d1curl, CURLINFO_HTTP_CODE);
echo $dropbox_upload;
echo $http_request;
curl_close($d1curl);
fclose($opendoc);

I wonder why dropbox is giving me for errors as I followed similiar instructions seen on stackoverflow for file send and also referenced the api explorer.

  • According to the PHP curl documentation, the 'CURLOPT_HEADER' option is to "to include the header in the output". It doesn't set the headers for the request. You should use the 'CURLOPT_HTTPHEADER' option for that instead.

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Staff rankDropbox Staff

    According to the PHP curl documentation, the 'CURLOPT_HEADER' option is to "to include the header in the output". It doesn't set the headers for the request. You should use the 'CURLOPT_HTTPHEADER' option for that instead.