Dropbox v2 API - large file uploads

喜欢而已 提交于 2019-11-29 18:11:16
smarx

It looks like curl_file_create encodes a file as a multipart form upload, which isn't what you want when talking to the Dropbox API.

If I understand correctly, the issue you're trying to address is that you don't want to load the entire file contents into memory. Is that right?

If so, please give the following a try. (Apologies that I haven't tested it at all, so it may contain mistakes.)

$headers = array('Authorization: Bearer Dropbox token',
                 'Content-Type: application/octet-stream',
                 'Dropbox-API-Arg: {"path":"/path/bigfile.txt",
                                    "mode":"add"}');

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

$path = '/path/to/bigfile.txt';
$fp = fopen($path, 'rb');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($path));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

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

echo $response;

Note that you may also want to consider /files/upload_session_start, etc. if you're uploading large files. That lets you upload in chunks, and it supports files bigger than 150MB (which /files/upload does not).

Eggy

If anyone interested a quick fix on Dropbox API v2 which CURLOPT_INFILE is not working, go to this link Dropbox PHP v2 upload issue

Greg explain and suggested to used this code curl_setopt($ch, CURLOPT_POSTFIELDS, fread($fp, $filesize)) instead of curl_setopt($ch, CURLOPT_INFILE, $fp);

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!