Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
curl_close($ch);
fclose($fp);

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

curl_setopt($ch,CURLOPT_ENCODING,'');
  • 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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading