function get_file_size( $url ) {
$curl = curl_init( $url );
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$info = curl_exec($curl);
curl_close($curl);
if (preg_match("/^HTTP\/1\.[01] (\d\d\d)/", $info, $matches)) {
$HTTP_status_code = (int)$matches[1];
}
if (preg_match("/Content-Length: (\d+)/", $info, $matches)) {
$file_size = (int)$matches[1];
}
$result = -1;
if ($HTTP_status_code == 200 || ($HTTP_status_code > 300 && $HTTP_status_code <= 308)) {
$result = $file_size;
}
return $result;
}
function download_file($url, $local_path) {
$remote_file = fopen($url, 'rb');
if ($remote_file) {
$local_file = fopen($local_path, 'wb');
if ($local_file) {
$file_size = get_file_size($url);
fwrite($local_file, fread($remote_file, $file_size), $file_size);
}
}
if ($remote_file) {
fclose($remote_file);
}
if ($local_file) {
fclose($local_file);
}
}
$url = 'https://coupondiscountblog.com/images/xcg.jpg';
$local_path = "d:\\xcg.jpg";
download_file($url, $local_path);
echo "Done.";
/*
run:
Done.
*/