We Want to Hear From You! What Do You Want to See on the Community? Tell us here!

Forum Discussion

Adrian D.2's avatar
Adrian D.2
Explorer | Level 3
10 years ago

How to download a file and save to local PC

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

Replies have been turned off for this discussion
  • Adrian D.2's avatar
    Adrian D.2
    Explorer | Level 3
    10 years ago

    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's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    10 years ago

    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's avatar
    Adrian D.2
    Explorer | Level 3
    10 years ago

    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.

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.6,036 PostsLatest Activity: 3 days ago
411 Following

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X or Facebook.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!