cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Back again with another edition of 'How we use Dropbox', so find out how Emma uses to-do lists to get it all done 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: 

UPLOAD FROM HTTP WITH PHP NOT WORKING

UPLOAD FROM HTTP WITH PHP NOT WORKING

Aziros
Explorer | Level 4

Hello ! I want to upload an image (test.png) from the root of my site to my Dropbox but it doesn't work. My php script is in the same folder than image test.png :

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://content.dropboxapi.com/2/files/upload");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
"file" => "@" .realpath("test.png")
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Authorization: Bearer MY_BEAUTIFUL_TOKEN";
$headers[] = "Dropbox-Api-Arg: {\"path\": \"/test.png\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}";
$headers[] = "Content-Type: application/octet-stream";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$curl_response_res = curl_exec ($ch);

echo $curl_response_res; // Server response
print_r(curl_getinfo($ch)); // Http Response
curl_close($ch);

fclose($fh_res);


What is the problem ?

2 Replies 2

Re: UPLOAD FROM HTTP WITH PHP NOT WORKING

Greg-DB
Dropboxer
What output are you getting? The HTTP response body from the API should contain either the metadata for the uploaded file, or an error indicating why the upload failed.

Re: UPLOAD FROM HTTP WITH PHP NOT WORKING

Aziros
Explorer | Level 4

Hello ! Thanks for interest, I found a solution on Stack Overflow :

$path = 'test.png';
		$fp = fopen($path, 'rb');
		$size = filesize($path);

		$cheaders = array('Authorization: Bearer <TOKEN>',
						  'Content-Type: application/octet-stream',
						  'Dropbox-API-Arg: {"path":"/'.$path.'", "mode":"add"}');

		$ch = curl_init('https://content.dropboxapi.com/2/files/upload');
		curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
		curl_setopt($ch, CURLOPT_PUT, true);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
		curl_setopt($ch, CURLOPT_INFILE, $fp);
		curl_setopt($ch, CURLOPT_INFILESIZE, $size);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$response = curl_exec($ch);

		echo $response;
		curl_close($ch);
		fclose($fp);

https://stackoverflow.com/documentation/dropbox-api/409/uploading-a-file/1354/uploading-a-file-via-c...

Poll
We love to learn from the educators who use Dropbox. Whether you teach kids, teens, adults or a combination of all three, we want to know what apps and integrations you use with Dropbox to help with teaching. Which of the ones below is your favorite, or most used tool?
Who's talking

Top contributors to this post

  • User avatar
    Aziros Explorer | Level 4
  • User avatar
    Greg-DB Dropboxer
What do Dropbox user levels mean?
Need more support?