繁体   English   中英

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

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

我是Dropbox API新手。 我已经完成了使用给定令牌将文件(在我的网站托管服务器上)上传到我的Dropbox的代码。 该过程成功运行。 我想设计一个页面,允许用户(从他/她的本地计算机)选择文件并直接上传到我的保管箱。

我有一个想法,控制器将首先将文件上传到主机,然后再上传到Dropbox。 但是,由于需要更多时间和带宽来完成,所以这个想法很糟糕。 上传后,我必须删除主机上的文件。

这是适用于我的主机的代码:

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

?>

我正在使用Codeigniter

我有一个想法,控制器将首先将文件上传到主机,然后再上传到Dropbox。 但是,由于需要更多时间和带宽来完成,所以这个想法很糟糕。

不幸的是,这是唯一安全的方法。 将文件添加到Dropbox时,需要知道您帐户的OAuth访问令牌,并且您不能让其他人知道该令牌。 (这将使他们可以控制帐户。)因此,令牌需要在您的服务器上保密。 这意味着文件需要上传到您的服务器,然后从那里传输到Dropbox。

暂无
暂无

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

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