Dropbox PHP v2上传问题 [英] Dropbox PHP v2 upload issue

查看:189
本文介绍了Dropbox PHP v2上传问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个 php cURL 代码,结果是成功但是返回大小为0的问题,可以任何人帮助我,我使用这个页面上的代码 Dropbox的V2 API - 大文件上传
我不知道为什么文件大小为零。任何人都可以帮助我吗?

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

谢谢,

更新(CODE):

  $文件名= '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);


解决方案

这是一个PUT请求,但必要时发送POST。

 <?php 

$ path ='test_php_upload.txt';
$ fp = fopen($ path,'rb');
$ size = filesize($ path);
$ b $ 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} 


I did a php cURL code, the result is success but the problem it return the size 0, can anyone help me out, I do use the code on this page Dropbox v2 API - large file uploads I'm not sure why the file size is zero. can anyone help me out?

Also this is the return from the request 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}

Thanks,

UPDATE (CODE):

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

解决方案

This version of the code seems to trick curl into acting like it's a PUT request, but has it send "POST" as necessary.

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

?>

That yields:

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

这篇关于Dropbox PHP v2上传问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆