You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.

Forum Discussion

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

Files Uploaded are Corrupted After api usage

I tried uploading various documents of types ms word doc and pdf to the site using the standard fread method in my curl postfields input. The files are corrupted and cannot be repaired by a word processor like libreoffice afterwards. I tried encoding the fread in base64, but the corruption still occured. How can I ensure my files are not corrupted when using the api and curl?

  • Greg-DB's avatar
    Greg-DB
    5 years ago

    I see you're adding a layer of base64 encoding via your call to base64_encode. You should not base64 encode the file data when uploading it. Try removing that base64_encode call.

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

    It sounds like there may be a problem with how you're transmitting or encoding the data for the upload call. Can you share the problematic code?

    • FSUInnovation's avatar
      FSUInnovation
      Explorer | Level 4
      $dropbox_url_upload = "https://content.dropboxapi.com/2/files/upload";
      	$opendoc = fopen($filename, 'rb');
      	$docsize = $doc['size'];
      	$d1curl = curl_init();
      	curl_setopt($d1curl, CURLOPT_URL, $dropbox_url_upload);
      	curl_setopt($d1curl, CURLOPT_TIMEOUT, $timeout);
      	curl_setopt($d1curl, CURLOPT_HTTPHEADER, [
      		utf8_encode('Authorization: Bearer ' . $dropbox_token),
                  'Content-Type: application/octet-stream',
                  'Dropbox-API-Arg: '.
                  json_encode(
                      array(
                          "path"=> $dropbox_directory,
                          "mode" => "add",
                          "autorename" => true,
                          "mute" => false
      	))]);
      	curl_setopt($d1curl, CURLOPT_POST, true);
      	curl_setopt($d1curl, CURLOPT_POSTFIELDS, base64_encode(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);
      • Greg-DB's avatar
        Greg-DB
        Icon for Dropbox Staff rankDropbox Staff

        I see you're adding a layer of base64 encoding via your call to base64_encode. You should not base64 encode the file data when uploading it. Try removing that base64_encode call.

About Discuss Dropbox Developer & API

Node avatar for Discuss Dropbox Developer & API

Make connections with other developers

795 PostsLatest Activity: 2 days ago
194 Following

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 or Facebook.

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!