Skip to content

Instantly share code, notes, and snippets.

@jmillerdesign
Created January 8, 2013 23:51
Show Gist options
  • Save jmillerdesign/4489197 to your computer and use it in GitHub Desktop.
Save jmillerdesign/4489197 to your computer and use it in GitHub Desktop.
Download a remote file to a local file
<?php
/**
* Download a remote file to a local file
*
* @param string $url URL to remote file
* @param string $localPathToFile Absolute path to local file where it will be saved
* @return boolean True if file saved successfully
*/
function downloadFile($url, $localPathToFile) {
$savedSuccessfully = false;
$remoteFile = fopen($url, 'rb');
if ($remoteFile) {
$localFile = fopen($localPathToFile, 'wb');
if ($localFile) {
while (!feof($remoteFile)) {
fwrite($localFile, fread($remoteFile, 1024 * 8), 1024 * 8);
}
fclose($localFile);
$savedSuccessfully = true;
}
fclose($remoteFile);
}
return $savedSuccessfully;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment