8

我有一个表格,允许用户填写几个方面,然后选择要上传的文件。

提交表单后,我想编写一些代码将文件保存到保管箱帐户并访问直接下载链接并将其放置在我托管的数据库中。

如果有人这样做过,是否有 API 的特定部分可供查看?或者有什么例子吗?

我似乎在 API 中找不到这个。

谢谢。

4

2 回答 2

17

根据我在 API 中看到的内容,可以做到这一点。您需要下载Dropbox Core API。在 zip 文件中,您将找到一个示例文件夹,其中包含用于身份验证、上传、下载、直接链接等的示例代码。只需查看direct-link.php并根据您的需要进行更改。这是一个经过测试的上传文件并生成直接下载链接的工作示例:

<?php

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

use \Dropbox as dbx;

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

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

$authorizeUrl = $webAuth->start();
echo "1. Go to: " . $authorizeUrl . "<br>";
echo "2. Click \"Allow\" (you might have to log in first).<br>";
echo "3. Copy the authorization code and insert it into $authCode.<br>";

$authCode = trim('DjsR-iGv4PAAAAAAAAAAAbn9snrWyk9Sqrr2vsdAOm0');

list($accessToken, $dropboxUserId) = $webAuth->finish($authCode);
echo "Access Token: " . $accessToken . "<br>";

$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);
// adding ?dl=1 to the link will force the file to be downloaded by the client.
$dw_link = $link[0]."?dl=1";

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

?>

我把它做得非常快只是为了让它工作。最终,您可能需要对其进行一些调整,以使其适合您的需求。

于 2014-01-12T19:27:53.190 回答
1

Core API 手册中有一节,请参阅链接。所以你可以像这样使用上传部分:

$f = file_get_contents('data.txt');
$result = $dbxClient->uploadFile("/data.txt", dbx\WriteMode::add(), $f);

echo 'file uploaded';
于 2014-01-12T19:37:06.810 回答