【问题标题】:Dropbox PHP v2 upload issueDropbox PHP v2 上传问题
【发布时间】:2015-12-10 10:59:00
【问题描述】:

我做了一个php cURL 代码,结果是成功但问题它返回大小为0,谁能帮助我,我确实使用此页面上的代码Dropbox v2 API - large file uploads 我不确定为什么文件大小为零。谁能帮帮我?

这也是请求 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}

谢谢,

更新(代码):

$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);

【问题讨论】:

  • 请分享您的代码。
  • 如果我的网站不使用 https 并且 cURL 没有附加认证,会不会有问题?
  • 我只是想添加更多细节,我发现了这个问题 stackoverflow.com/questions/24059681/… ,如果我运行 API,它会在我的脚本上发生同样的事情,它会为文件大小返回 0
  • 不,这不是问题。您可以记录文件的大小吗?例如。 echo filesize($fpath)。也许文件是空的?
  • 我已经做了并且有一个文件大小,它有 278kb

标签: php file-upload dropbox-api


【解决方案1】:

这个版本的代码似乎会欺骗 curl 让它看起来像是一个 PUT 请求,但它会根据需要发送“POST”。

<?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);

?>

产生:

{"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}

【讨论】:

  • 谢谢,花了将近 2 天的时间才弄明白这个
猜你喜欢
  • 2017-01-23
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-13
  • 1970-01-01
相关资源
最近更新 更多