Skip to content

Instantly share code, notes, and snippets.

@mohsin
Created August 7, 2019 19:57
Show Gist options
  • Save mohsin/2bc1401d8b7f6f5fc8739043cd541007 to your computer and use it in GitHub Desktop.
Save mohsin/2bc1401d8b7f6f5fc8739043cd541007 to your computer and use it in GitHub Desktop.
Laravel function to download files from Dropbox
public function download(Request $request, $clintId, $dtId, $inId)
{
$path = $request->input('dropbox_file_path');
$downloadFileName = basename($path);
$cheaders = [
'Authorization: Bearer ' . env('DROPBOX_BEARER_TOKEN'),
'Content-Type: text/plain',
'Dropbox-API-Arg: {"path":"$path"}'
];
$tmpfname = tempnam(storage_path() . '/app/public/', 'DOWNLOAD_');
$fp = fopen($tmpfname, 'w+');
$ch = curl_init('https://content.dropboxapi.com/2/files/download');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_POSTFIELDS, '');
$output = curl_exec($ch);
if ($output === false) {
echo "curl error: " . curl_error($ch);
}
curl_close($ch);
fclose($fp);
return response()->download($tmpfname, $downloadFileName, [])->deleteFileAfterSend(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment