Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
toki4004
3 years agoHelpful | Level 5
Upload string value to file - PHP
I need to upload a simple string via api - there is no "file" on the server - the string will be generated in my PHP code. First, does it need to be in binary? Second, when I upload, I get a "1" written into a file - and thats it. How do I get that string uploaded? I don't get any errors back. Help!
$fields = array(
"path" => "/ba.txt",
"autorename" => false,
"mode" => "overwrite",
"mute" => false,
"strict_conflict" => false );
$string = 'hello there';
$binary = strigToBinary($string).PHP_EOL;
$headers = array(
'Authorization: Bearer ' . $auth_token,
'Content-Type: application/octet-stream',
'Dropbox-API-Arg: '.json_encode($fields)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, 'https://content.dropboxapi.com/2/files/upload');
curl_setopt($ch, CURLOPT_INFILE, $binary);
and here is my binary function
function strigToBinary($string) {
$characters = str_split($string);
$binary = [];
foreach ($characters as $character) {
$data = unpack('H*', $character);
$binary[] = base_convert($data[1], 16, 2);
}
return implode(' ', $binary);
}
The /2/files/upload endpoint is a "content-upload" style endpoint, meaning that it expects the raw bytes of the file data in the HTTPS request body (with corresponding "application/octet-stream" type, as you have).
Exactly how you retrieve that data and configure your HTTPS client to send it like that is up to you. Please note that we can't provide support for configuring third party clients themselves, as they're not made by Dropbox, so you may need to refer to the documentation for the client you're using for information on how to configure it.
That said, looking at the code you shared, it looks like you might want to do the following, instead of your current 'CURLOPT_POSTFIELDS' and 'CURLOPT_INFILE' usage:
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
2 Replies
Replies have been turned off for this discussion
- Greg-DB3 years ago
Dropbox Community Moderator
The /2/files/upload endpoint is a "content-upload" style endpoint, meaning that it expects the raw bytes of the file data in the HTTPS request body (with corresponding "application/octet-stream" type, as you have).
Exactly how you retrieve that data and configure your HTTPS client to send it like that is up to you. Please note that we can't provide support for configuring third party clients themselves, as they're not made by Dropbox, so you may need to refer to the documentation for the client you're using for information on how to configure it.
That said, looking at the code you shared, it looks like you might want to do the following, instead of your current 'CURLOPT_POSTFIELDS' and 'CURLOPT_INFILE' usage:
curl_setopt($ch, CURLOPT_POSTFIELDS, $string); - toki40043 years agoHelpful | Level 5
Yes, that worked! Thanks,
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!