Last active
January 4, 2021 06:46
-
-
Save Vijaysinh/4f05383a759137746c15363bb6f79fda to your computer and use it in GitHub Desktop.
Download Files from Remote Server DB
This file contains 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
<?PHP | |
error_reporting(E_ALL); | |
ini_set('display_errors',1); | |
get_attachments(); | |
if(isset($_GET['id']) && !empty($_GET['id'])){ | |
$id = $_GET['id']; | |
$fileName = $_GET['file']; | |
download_file($id,$fileName); | |
} | |
function get_attachments($id = 1){ | |
$url = "https://site.com/1/attachment"; | |
$resource = curl_init(); | |
curl_setopt($resource,CURLOPT_HTTPHEADER,array( | |
"Site: user", | |
"Requestor: vijaysinh.parmar@local", | |
)); | |
curl_setopt($resource, CURLOPT_URL, $url); | |
curl_setopt($resource, CURLOPT_HEADER,false); | |
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($resource,CURLOPT_SSL_VERIFYPEER ,false); | |
curl_setopt($resource,CURLOPT_TIMEOUT,120); | |
$res = curl_exec($resource); | |
$decode = json_decode($res,true); | |
foreach($decode as $v){ | |
$fileName = $v['file']['fileName']; | |
$id = $v['id']; | |
echo "<a target='_blank' href='download.php?id=$id&file=$fileName'>".$fileName."</a>"; | |
echo "<br><br>"; | |
} | |
} | |
function download_file($id,$fileName){ | |
$url = "https://site.com/i/attachment/$id"; | |
$resource = curl_init(); | |
curl_setopt($resource,CURLOPT_HTTPHEADER,array( | |
"XX-PartnerSite: user", | |
"XX-Requestor: [email protected]", | |
)); | |
curl_setopt($resource, CURLOPT_URL, $url); | |
curl_setopt($resource, CURLOPT_HEADER,false); | |
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($resource, CURLOPT_BINARYTRANSFER, 1); | |
curl_setopt($resource,CURLOPT_SSL_VERIFYPEER ,false); | |
curl_setopt($resource,CURLOPT_FOLLOWLOCATION,true); | |
curl_setopt($resource,CURLOPT_AUTOREFERER,true); | |
curl_setopt($resource,CURLOPT_MAXREDIRS,10); | |
curl_setopt($resource,CURLOPT_TIMEOUT,120); | |
$filepath = $fileName; | |
$saveTo = $fileName; | |
$fp = fopen($saveTo, 'w+'); | |
curl_setopt($resource, CURLOPT_FILE, $fp); | |
$file = curl_exec($resource); | |
curl_close($resource); | |
fclose($fp); | |
header('Content-Description: File Transfer'); | |
header('Content-Type: application/octet-stream'); | |
header('Content-Disposition: attachment; filename="'.basename($filepath).'"'); | |
header('Content-Transfer-Encoding: binary'); | |
header('Expires: 0'); | |
header('Cache-Control: must-revalidate,post-check=0, pre-check=0'); | |
header('Pragma: public'); | |
header("Content-Length: " . filesize($filepath)); | |
header('X-Pad: avoid browser bug'); | |
ob_clean(); | |
flush(); | |
readfile($filepath); | |
unlink($filepath); | |
exit(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment