Upload file in Dropbox via PHP and cURL

The first thing of course is to have a valid Dropbox account and basic understanding of how a cURL request is built in PHP. Very helpful is the the Dropbox API Explorer where you can try various API calls and see the requests and the results. After you are sure that everything is working fine you can use or modify the following script:

$path = 'example.txt';
$fp = fopen($path, 'rb');
$size = filesize($path);

$cheaders = array('Authorization: Bearer YOUR_ACCESS_TOKEN,
                  'Content-Type: application/octet-stream',
                  'Dropbox-API-Arg: {"path":"/test/'.$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);

Thanks to RIP Tutorial for the script.