首页 文章

Dropbox在脚本中上传

提问于
浏览
8

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

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

如果有人这样做了,是否有API的特定部分可供查看?或者任何例子?

我似乎无法在API中找到这个 .

谢谢 .

2 回答

  • 1

    从我在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>";
    
    ?>
    

    为了让它正常工作,我做得非常快 . 最后,您可能需要稍微调整一下,以满足您的需求 .

  • 17

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

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

相关问题