Created
January 8, 2013 23:51
-
-
Save jmillerdesign/4489197 to your computer and use it in GitHub Desktop.
Download a remote file to a local file
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 | |
/** | |
* 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