I am uploading files using API v2 in php and getting the same issue with some selected files or file type, can you help me?
I'll be happy to help with any issues you're having with the Dropbox API, but I'll need some more information. Please reply with:
$fileURL = 'http://localhost/wordpress/wp-content/uploads/2021/05/my-file.txt';
$tempFilePath = basename($fileURL);
try{$path = $tempFilePath;$fp = fopen($path, 'rb');$size = filesize($path);$cheaders = array('Authorization: Bearer <ACCESS_TOKEN>','Content-Type: application/octet-stream','Dropbox-API-Arg: {"path":"/'.$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);$resp = curl_exec($ch);curl_close($ch);fclose($fp);$jsonData= $resp; // put here your json object$arrayData = json_decode($jsonData, true);if($arrayData['size'] != 0 ){print_r($arrayData);// Removed file from this server atfer moved in dropbox Storage.wp_delete_attachment($dt['upload_file_id'] );
$response = array('status' => 'success');$response['success'] = true;}
I am getting this output:-
array printed:
Array([name] => my-file.txt[path_lower] => /my-file.txt[path_display] => /my-file.txt[id] => id:B5l9vJBpsTAAAAAAAAAAbQ[client_modified] => 2021-05-28T11:30:07Z[server_modified] => 2021-05-28T11:30:07Z[rev] => 015c36232ad2ec4000000019de99780[size] => 0[is_downloadable] => 1[content_hash] => 1dc1597f40484ade184602a580c89f61adcedf3c5be81c74e31748ba36f2d5b9)
Response which I am getting :
{"status":"success","success":true,"dtarray":"{\"name\": \"s12-compressor.jpg\", \"path_lower\": \"\/s12-compressor.jpg\", \"path_display\": \"\/s12-compressor.jpg\", \"id\": \"id:B5l9vJBpsTAAAAAAAAAAbQ\", \"client_modified\": \"2021-05-28T11:30:07Z\", \"server_modified\": \"2021-05-28T11:30:07Z\", \"rev\": \"015c36232ad2ec4000000019de99780\", \"size\": 0, \"is_downloadable\": true, \"content_hash\": \"1dc1597f40484ade184602a580c89f61adcedf3c5be81c74e31748ba36f2d5b9\"}"}
I just gave this a try myself, plugging in my own access token and local file path, and it successfully uploaded a non-empty file as expected for me.
You should check that your local file is non-empty and is being accessed successfully. For instance, check what the value of "$size" is for you, and make sure your "$fileURL", "$tempFilePath", and "$path" values are correct.
Thanks Sometimes this code not working and a blank file with 0kb is uploaded on dropbox sever.
Have you checked the suggestions in my last message?