cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Back again with another edition of 'How we use Dropbox', so find out how Emma uses to-do lists to get it all done 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
New member | Level 2

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

Re: How to download a file and save to local PC

Greg-DB
Dropboxer

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

Re: How to download a file and save to local PC

Adrian D.2
New member | Level 2

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 ?

Re: How to download a file and save to local PC

Greg-DB
Dropboxer

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. 

 

Re: How to download a file and save to local PC

Adrian D.2
New member | Level 2

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.

Poll
We love to learn from the educators who use Dropbox. Whether you teach kids, teens, adults or a combination of all three, we want to know what apps and integrations you use with Dropbox to help with teaching. Which of the ones below is your favorite, or most used tool?
Who's talking

Top contributors to this post

  • User avatar
    Adrian D.2 New member | Level 2
  • User avatar
    Greg-DB Dropboxer
What do Dropbox user levels mean?
Need more support?