Dropbox API Uploading Document Size Is Zero

480 Views Asked by At

I am using PHP and Curl to upload a file into Dropbox but everytime it is creating empty files. Here is my code:

        // reading the remote file to get the file size...
        $file = 'https://upload.wikimedia.org/wikipedia/en/5/58/Penny_test.jpg';
        $ch = curl_init($file);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_exec($ch);
        $fileSize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);  // Response is 11201 bytes

        // upload file to dropbox
        $token = 'xxxxxxx'
        $headers = [
            "Authorization: Bearer ". $token,
            'Dropbox-API-Arg: {"path": "/Penny_test.jpg", "mode": "add", "autorename": true, "mute":false}',
            "Content-Type: application/octet-stream",
        ];

        $url = 'https://content.dropboxapi.com/2/files/upload';
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_POST, true);

        $file = 'https://upload.wikimedia.org/wikipedia/en/5/58/Penny_test.jpg';
        $fp = fopen($file, 'rb');
        curl_setopt($ch, CURLOPT_INFILE, $fp);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_INFILESIZE, $fileSize);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        $result = curl_exec($ch);
        print_r($result); die;

Here's the response that I get:

{"name": "54.png", "path_lower": "/penny_test.jpg", "path_display": "/Penny_test.jpg", "id": "id:xxxxxxxxxxxxxxx", "client_modified": "2017-12-04T23:35:37Z", "server_modified": "2017-12-04T23:35:37Z", "rev": "bb3a7696db", "size": 0, "content_hash": "xxxxxxxxxxxxxxxxxxxxxxxxxx"}

Why is Dropbox get 0 sized files from this upload & what am I doing wrong?

Thanks.

1

There are 1 best solutions below

0
On

The issue here does seem to be how the file data isn't being supplied in the request body properly.

To fix it, replace:

curl_setopt($ch, CURLOPT_POST, true);

with:

curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST')