我正在使用PHP和Curl将文件上传到Dropbox,但每次创建空文件时。这是我的代码:
// 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;
以下是我得到的回复:
{"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"}
为什么Dropbox会从此上传文件中获取0个大小的文件我究竟做错了什么?
感谢。
答案 0 :(得分:0)
这里的问题似乎是如何正确地在请求正文中提供文件数据。
要修复它,请替换:
curl_setopt($ch, CURLOPT_POST, true);
使用:
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST')