cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Discuss Dropbox Developer & API

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: Files Uploaded are Corrupted After api usage

Files Uploaded are Corrupted After api usage

FSUInnovation
Explorer | Level 4
Go to solution

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?

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

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.

View solution in original post

5 Replies 5

Greg-DB
Dropbox Staff
Go to solution

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
Explorer | Level 4
Go to solution
$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
Dropbox Staff
Go to solution

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.

FSUInnovation
Explorer | Level 4
Go to solution

It is working now and am able to view the contents on my dropbox viewer. However, when I tried doing it without any encoding yesterday, I also had corruption. What could increase the risk of file corruption when uploading with the api?

Greg-DB
Dropbox Staff
Go to solution

There's no reason an upload without extra encoding like that should result in a corrupted file.

If you can reproduce an issue where correct code and a valid original file results in a corrupted file when uploaded to Dropbox via the API, please share the steps and code to reproduce it so we can look into it.

Need more support?