Problem with download file via CURL in PHP

I have a problem when i try download file with curl php.

Headers response

enter image description here

PHP Code

$filePath = $this->exchangePath . $this->exchangeFile;
$url = $this->exchangeURL;

$fp = fopen($filePath, 'w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);

Preview file data after downloading (106Kb)

enter image description here

Preview file data on server (1.9 Mb – The file will be bigger in the future. Maybe 300Mb or large.)

enter image description here

Help me, please.

>Solution :

the problem is that you’re ignoring that Content-Encoding: gzip header. The server returns the data gzip-encoded, and you’re not un-gzipping it.

Luckily for you, curl has built-in support for gzip decoding (curl has basically supported gzip and deflate decoding forever, but modern versions of curl also support br and zstd)

to enable automatic decoding of supported encodings, simply set

  • setting CURLOPT_ENCODING to emptystring turns on automatic decoding of all supported encodings.

aalso, you should use the fopen binary mode wb so Windows doesn’t fuck with your \n bytes.

Leave a Reply