How can I download a file from PHP code from any server?
Asked
Active
Viewed 2.1k times
3
-
1Check out this page http://php.net/manual/en/function.fpassthru.php – psalvaggio Apr 10 '12 at 12:38
-
Care to elaborate a bit more? A link to another page with no explanation isn't really an answer. – David Apr 10 '12 at 12:39
-
For most file downloads of small to medium size, `fpassthru()` is a convenient way to send the file over HTTP. The code in Example #1 on this page should be a pretty simple solution. – psalvaggio Apr 10 '12 at 12:43
1 Answers
5
You can use Curl to download file from web using php
function curl_get_file_contents($URL) {
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
$err = curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);
if ($contents) return $contents;
else return FALSE;
}
pass url to this function and download contents. alternatively you can use file reader/writer
private function downloadFile ($url, $path) {
$newfname = $path;
$file = fopen ($url, "rb");
if ($file) {
$newf = fopen ($newfname, "wb");
if ($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
}