0

We have a backend system where on a form fill, a PDF is generated - this PDF is then uploaded to dropbox and a link to the file is retrieved which is then added to the database and is visible to our vendors.

I have looked high and low - but the only way to get these links is using the createTemporaryDirectLink directive. However, these links are temporary and expire after a few hours.

I am looking to procure sharable non-expiring links.

Here is my current code that works fine

// Dropbox //
        $dropbox_config = array(
        'key'    => 'xxxx',
        'secret' => 'xxxx'
        );

        $accessToken='xxx-xxxxx';

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


        $dbxClient = new dbx\Client($accessToken, "PHP-Example/1.0");

        $sqlQuery = "select dropboxLink from get_order where orderId='".$lastOrderId."'";
        $rsGetDropLink = mysqli_query($dbLink,$sqlQuery);
        $rowGetDropLink =  mysqli_fetch_assoc($rsGetDropLink);

        $f = fopen("Orders/".$rowGetDropLink['dropboxLink'].'.pdf', "rb");
        $result = $dbxClient->uploadFile("/Orders/".$rowGetDropLink['dropboxLink'].'.pdf', dbx\WriteMode::add(), $f);
        fclose($f);
        print_r($result);

        $file = $dbxClient->getMetadata("/Orders/".$rowGetDropLink['dropboxLink'].'.pdf');
        $dropboxPath = $file['path'];
        $pathError = dbx\Path::findError($dropboxPath);
        if ($pathError !== null) {
        fwrite(STDERR, "Invalid <dropbox-path>: $pathError\n");
        die;
        }
        $link = $dbxClient->createTemporaryDirectLink($dropboxPath);
        $dw_link = $link[0]."?dl=1";

        $sqlQuery = "update get_order set fullDropboxLink='".$dw_link."' where orderId='".$lastOrderId."'";
        mysqli_query($dbLink,$sqlQuery);

        // Dropbox ends//
        header("location: thankyou.php?ordId=".$lastOrderId.'&categoryId='.$_REQUEST['categoryId']);

As you can see, we use the createTemporaryDirectLink method which leads to a temporary link being fetched which expired overtime.

Our vendors check these files often every two days - so this is a non-usable solution.

1 Answers1

0

Instead of using the createTemporaryDirectLink method, you can use the createShareableLink method:

https://dropbox.github.io/dropbox-sdk-php/api-docs/v1.1.x/class-Dropbox.Client.html#_createShareableLink

This will give you a shared link that doesn't expire. There are a few things to note though:

  • These links can be revoked.
  • The link will show on the user's https://www.dropbox.com/links page.
  • By default, these links point to a preview page, not the file content itself.

The last point can be addressed as shown here though:

https://www.dropbox.com/help/201

Greg
  • 16,359
  • 2
  • 34
  • 44
  • Hi Greg, should I just replace createTemporaryDirectLink with createShareableLink or will the code need an overhaul as well? – user1595112 Apr 19 '16 at 19:12
  • The return values are slightly different so you'll need to update your code to match. – Greg Apr 19 '16 at 20:10