Created
August 7, 2019 19:57
-
-
Save mohsin/2bc1401d8b7f6f5fc8739043cd541007 to your computer and use it in GitHub Desktop.
Laravel function to download files from Dropbox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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