将文件上传到Dropbox文件夹 [英] Upload a file into dropbox folder

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

问题描述

我尝试过将文件从桌面上的文件夹上传到保管箱帐户中的文件夹。

I have tried this to upload a file from a folder in my desktop to a folder in dropbox account.

但是每次我通过此文件上传空文件时代码。

but each time i have uploaded an empty file through this code.

怎么可能?

在我的代码下面:

     $ch = curl_init();
     $TOKEN = "asasasa";//token here
     $url = 'https://content.dropboxapi.com/2/files/upload';
     curl_setopt($ch,CURLOPT_URL,$url);
     curl_setopt($ch,CURLOPT_POST, 1);             
     curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
     $headers = array();
     $headers[] = 'Accept: application/json';
     $headers[] = 'Content-Type: multipart/form-data';
     $headers[] = 'Dropbox-API-Arg:{"path":"/home/new/ofd","mode":{".tag":"add"},"autorename":false,"mute":false}';
     $headers[] = "Authorization: Bearer ".$TOKEN;
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);     
     $response = curl_exec($ch);  
     $droplist  = json_decode($response,true);


推荐答案

您似乎没有添加文件内容到任何地方的上传调用,因此代码中都应该有一个空文件。

You don't seem to be adding the file content to the upload call anywhere, so an empty file is expected from your code.

您可以在下面的PHP中找到使用/ 2 / files / upload和curl的示例。使用 CURLOPT_INFILE 本身添加文件内容。

You can find an example of using /2/files/upload with curl in PHP below. That uses CURLOPT_INFILE to add the file content itself.

<?php

$path = 'test_php_upload.txt';
$fp = fopen($path, 'rb');
$size = filesize($path);

$cheaders = array('Authorization: Bearer <ACCESS_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);

?>

< ACCESS_TOKEN> OAuth 2访问令牌。

<ACCESS_TOKEN> should be replaced with the OAuth 2 access token.

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

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