Invalid MD5 checksum after loading from CURL in PHP - file_get_contents works fine

I have a script where I need to upload some files and make sure everything is working fine. I am comparing MD5 checksums.

I found that the checksums are incorrect when loading using CURL. The following is a description of the script. It downloads the Google logo and compares checksums.

$url = 'http://www.google.com/intl/en_ALL/images/logo.gif';

echo md5_file($url)."\n";

$path = 'f1';
file_put_contents($path, file_get_contents($url));
echo md5_file($path)."\n";


$path = 'f2';
$out = fopen($path, 'wb');
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
curl_close($ch);
echo md5_file($path)."\n";

conclusion:

e80d1c59a673f560785784fb1ac10959
e80d1c59a673f560785784fb1ac10959
d83892759d58a1281e3f3bc7503159b5

The first two are correct (they correspond to the MD5 checksum when I load the logo using firefox), and the result created by the curl is not true.

any ideas how to fix this?

thanks for the help

UPDATE:

, . . , , , , .

$path = 'f3';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
file_put_contents($path, curl_exec ($ch));
echo md5_file($path)."\n";
curl_close ($ch);
+3
2

fclose($out), md5_file, .

+1

curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

+1

Source: https://habr.com/ru/post/1728197/


All Articles