Skip to content

Instantly share code, notes, and snippets.

@xcoders-hub
Forked from Vijaysinh/file_download.php
Created January 4, 2021 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xcoders-hub/a21d6904d435b370b548ef74ffd573f6 to your computer and use it in GitHub Desktop.
Save xcoders-hub/a21d6904d435b370b548ef74ffd573f6 to your computer and use it in GitHub Desktop.
Download Files from Remote Server DB
<?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: vijaysinh.parmar@local.com",
));
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