简体   繁体   English

使用PHP API从用户的计算机上载文件到Dropbox

[英]Upload File to Dropbox from User's Machine using PHP API

I am newbie to Dropbox API . 我是Dropbox API新手。 I have completed the code which upload the file (on my website host) to my Dropbox with a given token. 我已经完成了使用给定令牌将文件(在我的网站托管服务器上)上传到我的Dropbox的代码。 The process runs successfully. 该过程成功运行。 I want to design a page which allows the user select a file (from his/her local machine) and upload directly to my dropbox. 我想设计一个页面,允许用户(从他/她的本地计算机)选择文件并直接上传到我的保管箱。

I have an idea that the controller will upload the file to host first then upload to dropbox. 我有一个想法,控制器将首先将文件上传到主机,然后再上传到Dropbox。 However this idea sucks as it takes more time and bandwidth to complete. 但是,由于需要更多时间和带宽来完成,所以这个想法很糟糕。 And I have to delete the file on host after uploading. 上传后,我必须删除主机上的文件。

This is the code which works on my host: 这是适用于我的主机的代码:

<?php

require_once "dropbox-sdk/lib/Dropbox/autoload.php";

use \Dropbox as dbx;

$dropbox_config = array(
    'key'    => 'my key',
    'secret' => 'my secret key'
);

$appInfo = dbx\AppInfo::loadFromJson($dropbox_config);
$webAuth = new dbx\WebAuthNoRedirect($appInfo, "PHP-Example/1.0");


$accessToken = 'my token code is given here';
$dbxClient = new dbx\Client($accessToken, "PHP-Example/1.0");

// Uploading the file
$f = fopen("working-draft.txt", "rb");
$result = $dbxClient->uploadFile("/working-draft.txt", dbx\WriteMode::add(), $f);
fclose($f);
//print_r($result);

// Get file info
$file = $dbxClient->getMetadata('/working-draft.txt');

// sending the direct link:
$dropboxPath = $file['path'];
$pathError = dbx\Path::findError($dropboxPath);
if ($pathError !== null) {
    fwrite(STDERR, "Invalid <dropbox-path>: $pathError\n");
    die;
}

// The $link is an array!
$link = $dbxClient->createTemporaryDirectLink($dropboxPath);
$dw_link = $link[0]."?dl=1";

echo "Download link: ".$dw_link."<br>";

?>

And I am using Codeigniter 我正在使用Codeigniter

I have an idea that the controller will upload the file to host first then upload to dropbox. 我有一个想法,控制器将首先将文件上传到主机,然后再上传到Dropbox。 However this idea sucks as it takes more time and bandwidth to complete. 但是,由于需要更多时间和带宽来完成,所以这个想法很糟糕。

Unfortunately, this is the only secure way to do this. 不幸的是,这是唯一安全的方法。 Adding a file to your Dropbox requires knowing an OAuth access token for your account, and you can't allow others to know that token. 将文件添加到Dropbox时,需要知道您帐户的OAuth访问令牌,并且您不能让其他人知道该令牌。 (It would give them control of the account.) So the token needs to be kept secret, on your server. (这将使他们可以控制帐户。)因此,令牌需要在您的服务器上保密。 That means the file needs to be uploaded to your server and transferred to Dropbox from there. 这意味着文件需要上传到您的服务器,然后从那里传输到Dropbox。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM