Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropbox: Produce a direct download link [PHP preferred]

I'm using the Dropbox REST API and I can successfully retrieve a share url for a file.

https://www.dropbox.com/developers/reference/api#shares

However, the share link takes the user to a preview page on dropbox.com, whereas I'm looking for a direct link that a user could directly download a file. eg. Right click, Save as...

like image 474
Paul Avatar asked Oct 29 '12 03:10

Paul


People also ask

How do I create a direct download link in Dropbox?

Go to Dropbox.com, find your file, and click the Copy link button that appears when you hover over it. Or, on your desktop, right-click on the file, and select Copy Dropbox Link. Copy that link and paste it in your browser, and it should download the file directly.

Does Dropbox View link allow download?

You can share this link through Dropbox or by pasting the link into an email or chat. Recipients can preview the file on dropbox.com, but the Download button will be grayed out and won't be clickable. If a recipient hovers over Download button, they'll see “Downloads disabled.”

How do I download files from Dropbox without an account?

Can I receive shared files when I don't have a Dropbox account? You don't need a Dropbox account to view the files in a shared link, and you can download those files to your computer. Files that you download from a shared link won't sync with Dropbox if you edit them.


1 Answers

It turns out that the default share url that is returned is a short url and the short url will always point to the Dropbox preview page.

Therefore, you need to get the REST API to return the full url by setting the short_url parameter to false. Once you have the full url, then add ?dl=1 at the end of url.

Eg: https://dl.dropbox.com/s/xxxxxxxxxxxxxxxxxx/MyFile.pdf?dl=1

More info:

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

Prompt user to save on download from Dropbox

PHP Example:

This example has borrowed/inspired-by code samples from these: http://www.phpriot.com/articles/download-with-curl-and-php

http://www.humaan.com.au/php-and-the-dropbox-api/

/* These variables need to be defined */
$app_key = 'xxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxxxxxxxx';
$user_oauth_access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$user_oauth_access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';    

$ch = curl_init(); 

$headers = array( 'Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT"' );

$params = array('short_url' => 'false', 'oauth_consumer_key' => $app_key, 'oauth_token' => $user_oauth_access_token, 'oauth_signature' => $app_secret.'&'.$user_oauth_access_token_secret);

curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $params);
curl_setopt( $ch, CURLOPT_URL, 'https://api.dropbox.com/1/shares/'.$dir );

/*
* To handle Dropbox's requirement for https requests, follow this:
* http://artur.ejsmont.org/blog/content/how-to-properly-secure-remote-api-calls-from-php-application
*/
curl_setopt( $ch, CURLOPT_CAINFO,getcwd() . "\dropboxphp\cacert.pem");
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, TRUE);

curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
$api_response = curl_exec($ch);

if(curl_exec($ch) === false) {
    echo 'Curl error: ' . curl_error($ch);
}

$json_response = json_decode($api_response, true);

/* Finally end with the download link */
$download_url = $json_response['url'].'?dl=1';
echo '<a href="'.$download_url.'">Download me</a>';
like image 130
Paul Avatar answered Sep 28 '22 16:09

Paul