Skip to content

Instantly share code, notes, and snippets.

@dilakv
Created May 31, 2021 18:12
Show Gist options
  • Save dilakv/2841c8e26219ce4d6a4d9beae5dd4faf to your computer and use it in GitHub Desktop.
Save dilakv/2841c8e26219ce4d6a4d9beae5dd4faf to your computer and use it in GitHub Desktop.
Download a file from server using php and curl
<?php
// create a new cURL resource
$ch = curl_init();
$URL = "HTTP - Direct-Link";
$filename = "nameofthefile.extension";
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$out = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
$fp = fopen($filename, 'w');
fwrite($fp, $out);
fclose($fp);
echo "Hecho!";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment