douhuanbai6729 2017-12-04 23:50
浏览 96

Dropbox API上传文档大小为零

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条回答 默认 最新

  • dongtiao2105 2017-12-05 16:53
    关注

    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')
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件