cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Are you interested in hearing how one of our Community members uses Dropbox for sailing trips? Read all about it 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 string value to file - PHP

Upload string value to file - PHP

toki4004
Explorer | Level 4
Go to solution

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);    
}

 

 

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

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);

 

View solution in original post

2 Replies 2

Greg-DB
Dropbox Staff
Go to solution

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);

 

toki4004
Explorer | Level 4
Go to solution

Yes, that worked! Thanks,

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    toki4004 Explorer | Level 4
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?