How to solve cUrl corrupting file during download

孤街浪徒 提交于 2019-12-24 14:28:30

问题


I really, really need help as to how to solve this issue I'm having:

Using script:

<?php
    $curl = curl_init();
    $fp = fopen("somefile.zip", "w");
    curl_setopt ($curl, CURLOPT_URL, "http://website.com/test.zip");
    curl_setopt($curl, CURLOPT_FILE, $fp);
    curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_exec ($curl);
    curl_close ($curl);

I have asked before, and no-one seems to have a solution as to how I solve this... If someone can even tell me why it's happening ie. is it file size, binary transfer etc. I can work with that!

The file ZIP file downloads and creates somefile.zip but the XML file within is partially corrupt.

Sample:

K#Teº22)dVTð¼ÜvØ
rÏ*HIê±dE*¬òPÜÊâR}ÝbJÉÂX:Î@z|Eª2Ér  tk­2UÄOK¼É,·,­Ûs¦ê1Z°VÝk6Ù«ËGÝw©5Æ]ÛQcq¥¼½ØïÒÐ]êÈy¨ð¶Çùûü]ÛßþW¤ùâÝÀw|~§ïúÁ¸ÛHBq®*YtrÛÕiî$   /ñ¥n?è¶;_ò
É¡ä ç&ýOr óß)yÿ¤$+`~TÙAófHU ¢SÝvW¶¦xA5Å׶Ãrå<8^ÐË4w­ qz Ø«<Ñ"*ººÝ?èO^;ÃQûÉOÏÀ¾?ìw|Õ±¥©3w©Ýr£ ÃÊÀ ¿^Á^UÛLß_ôÜÎh4îÖWcíF^8¾ö÷ؼ¾¿`âX3Ûú^{  À<.Æ¡(±1f¢.¸®k/ìÝeÓçê'PAnÓõ¸K`TeQ÷b|'¥Ñ)1ÓãnsÞèàÎZ|ê*+kuw×cªëÇ:§$¤ã¸Î1ü±Úh6ÕÀQ¦©D4Âp4b{Èo¾
,4"R

回答1:


Can you set CURLOPT_HEADER to 0 and try again?

Edit:

Or try this:

$url  = 'http://website.com/test.zip';
$path = 'somefile.zip';


$ch = curl_init($url);
if($ch === false)
{
    die('Failed to create curl handle');
}

$fp = fopen($path, 'w');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);

$data = curl_exec($ch);

curl_close($ch);
fclose($fp);


来源:https://stackoverflow.com/questions/21285025/how-to-solve-curl-corrupting-file-during-download

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!