cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to download a file and save to local PC

How to download a file and save to local PC

Adrian D.2
Explorer | Level 3

I am new to using php and dropbox, so this query may seem simple.

My application is to integrate the ability to upload/download to a specified folder in my Dropbox.

I can do the following in php (with the help of CodeCourse)

  • authenticate
  • list my files in a given folder
  • upload a file to a given folder

I am struggling with how to download a file to my PC and save it.

Code snippet:

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

$children = $client->getMetadataWithChildren($path);

$docs = $children["contents"];

# from the $docs array I can get the path to the required file

# I then try

 

$outStream ='';

$download = $client->getFile($path,$outStream);
header("Content-type: " . $download["mime"]);
echo $download["data"];
exit;

 

I think the headers may need to be improved but the first problem is that the getFile function errors as I am not initialising $outStream correctly.

 

Any help would be appreciated

4 Replies 4

Greg-DB
Dropbox Staff

It looks like you're using the Dropbox PHP Core SDK. The tutorial for that covers downloading a file using the getFile method here:

https://www.dropbox.com/developers-v1/core/start/php#downloading

Adrian D.2
Explorer | Level 3

Thank you , I had seen this but discounted it as it stores on my web server , whereas i really it want it served to my browser where the user can save to local disk ?

Greg-DB
Dropbox Staff

In that case, you essentially have two options:

1) Use getFile to get the file content and pass it down to the browser instead of saving it to disk. You'll still need to supply a valid resource as the $outStream parameter where the SDK can write the data though. How exactly you set that up it outside the scope of Dropbox API support, and may depend on your web framework, so I can't offer much insight there.

2) If you don't want to pass the data through your server, use createShareableLink or createTemporaryDirectLink to get a link to the file to give to the user in their browser. 

 

Adrian D.2
Explorer | Level 3

In case this might be of help to others, I decided to use the sdk suggested route and save to web server using a temp file, which I create with a new stream for the getFile method. After downloading the file I then clear the file.

thanks.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Adrian D.2 Explorer | Level 3
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?