在脚本中上传Dropbox [英] Dropbox uploading within script

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

问题描述

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

I have a form that allows a user to fill in several aspects and then choose a file to upload.

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

When the form is submitted, I want write some code that saves the file to a dropbox account and gets access to a direct download link and places this in a database I am hosting.

如果有人这样做,API的特定部分是否值得关注?还是任何例子?

If anyone has done this, is there a specific section of the API to look at? Or any examples?

我似乎无法在API中找到它.

I can't seem to find this in the API.

谢谢.

推荐答案

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

From what I see in the API it is possible to do this. You need to download the Dropbox Core API. Inside the zip file, you will find an example folder with example code for authentication, upload, download, direct-link and so on. Just see the direct-link.php and change it to your needs. Here is a tested working example of uploading a file and generating a direct link for download:

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

?>

为了使它正常工作,我很快就做到了.最终,您可能需要对其进行一些调整,以使其适合您的需求.

I made this really fast just to get it working. Eventually you may need to tweak it a bit so it will suite your needs.

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

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