Forum Discussion

krupal's avatar
krupal
Explorer | Level 3
8 years ago

Dropbox nested files download using CURL

Hi Support,

 

I am downloading files using below function where I am able to download only root files. I am facing issue in downloading files under folder

 

 

function dbx_get_file($token, $in_filepath, $out_filepath)
{


$out_fp = fopen($out_filepath, 'w+');

if ($out_fp === FALSE)
{
echo "fopen error; can't open $out_filepath\n";
return (NULL);
}
$url = 'https://content.dropboxapi.com/2/files/download';
$header_array = array(
'Authorization: Bearer ' . $token,
'Content-Type:',
'Dropbox-API-Arg: {"path":"' . $in_filepath . '"}'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header_array);
curl_setopt($ch, CURLOPT_FILE, $out_fp);
$metadata = null;
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header) use (&$metadata)
{
$prefix = 'dropbox-api-result:';
if (strtolower(substr($header, 0, strlen($prefix))) === $prefix)
{
$metadata = json_decode(substr($header, strlen($prefix)), true);
}
return strlen($header);
}
);
$output = curl_exec($ch);
if ($output === FALSE)
{
echo "curl error: " . curl_error($ch);
}
curl_close($ch);
 
fclose($out_fp);
return($metadata);

} // dbx_get_file()
if(isset($_GET['download']))
{
$file = $_GET['download'];
$metadata = dbx_get_file("token", '/'.$file, $file); 

 

In above code when file under folder is passing for example 'test/abc.png' then it shows error in fopen() + unable to open file/folder.

 

Please help me to resolve this issue asap

 

1 Reply

  • Greg-DB's avatar
    Greg-DB
    Icon for Dropbox Community Moderator rankDropbox Community Moderator
    8 years ago
    It sounds like that's an issue with using `fopen` and not the Dropbox side of things, so I'm afraid I can't offer much help. You'll need to debug why you can't `fopen` your local file. (E.g., make sure the parent folders exist, that you have permission to access it, etc.)

About Dropbox API Support & Feedback

Node avatar for Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.

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, Facebook or Instagram.

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!