0

I did a php cURL code, the result is success but the problem it return the size 0, can anyone help me out, I do use the code on this page Dropbox v2 API - large file uploads I'm not sure why the file size is zero. can anyone help me out?

Also this is the return from the request cURL

{"name": "2.jpg", "path_lower": "/images/2.jpg", "id": "id:92FZUH08Y6AAAAAVVAAAEA", "client_modified": "2015-12-10T11:02:38Z", "server_modified": "2015-12-10T11:02:38Z", "rev": "1c40f677f1", "size": 0}

Thanks,

UPDATE (CODE):

$filename='2.jpg';
$cheaders = array('Authorization: Bearer =TOKEN=','Content-Type: application/octet-stream','Dropbox-API-Arg: {"path":"/images/'.$filename.'", "mode":"add"}');
$ch = curl_init('https://content.dropboxapi.com/2/files/upload');
curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders );
curl_setopt($ch, CURLOPT_POST, true);
$fpath = '/home2/public_html/uploads/'.$filename;
$fp = fopen($fpath, 'rb');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($fpath));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
fclose($fp);
Community
  • 1
  • 1
Eggy
  • 103
  • 1
  • 2
  • 13
  • Please share your code. – user94559 Dec 10 '15 at 11:57
  • is it an issue if my site don't use https and the cURL has no certification attach? – Eggy Dec 10 '15 at 13:38
  • I just want to add more details, this issue I found http://stackoverflow.com/questions/24059681/dropbox-core-api-0-bytes-size-in-metadata-for-image?rq=1 , its the same thing happens to my script if I run the API it return 0 for the file size – Eggy Dec 10 '15 at 18:14
  • No, that wouldn't be an issue. Can you log the size of the file? E.g. `echo filesize($fpath)`. Maybe the file's empty? – user94559 Dec 10 '15 at 18:15
  • I already did and has a filesize, it has 278kb – Eggy Dec 10 '15 at 18:15
  • I think the issue is that `CURLOPT_INFILE` is only by supported by curl for PUT, but the API only uses POST. I can't find a better way to do this than `curl_setopt($ch, CURLOPT_POSTFIELDS, fread($fp, $filesize));` but that's not good for large files of course. – Greg Dec 10 '15 at 20:51
  • @Greg thanks that works but as you said it is not properly working on large files, I will have to work it on upload session. Thanks, I will try to contact dropbox support to ask them for possible solution for CURLOPT_INFILE – Eggy Dec 10 '15 at 21:03
  • I am Dropbox developer support, but unfortunately the matter of `CURLOPT_INFILE` is about curl and not Dropbox itself, so I can't offer much help there in particular. – Greg Dec 10 '15 at 21:28
  • Ah, nevermind, we figured out a way. I'm posting an answer. – Greg Dec 10 '15 at 22:35

1 Answers1

2

This version of the code seems to trick curl into acting like it's a PUT request, but has it send "POST" as necessary.

<?php

$path = 'test_php_upload.txt';
$fp = fopen($path, 'rb');
$size = filesize($path);

$cheaders = array('Authorization: Bearer =TOKEN=',
                  'Content-Type: application/octet-stream',
                  'Dropbox-API-Arg: {"path":"/test/'.$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);
$response = curl_exec($ch);

echo $response;
curl_close($ch);
fclose($fp);

?>

That yields:

{"name": "test_php_upload.txt", "path_lower": "/test/test_php_upload.txt", "id": "id:25N5ksooX-sAAAAAAAHcWg", "client_modified": "2015-12-10T22:35:07Z", "server_modified": "2015-12-10T22:35:07Z", "rev": "56384021eccc7", "size": 15}
Greg
  • 16,359
  • 2
  • 34
  • 44