How to use php to download a zip archive?

Help please, can not download the php zip archive which is on the server I have.
here is what I tried but to no avail.
header('Content-type: application/zip');
 header('Content-Disposition: attachment; filename="'.basename($zipFile).'"');
 header("Content-length:" . filesize($zipFile));
 header("Pragma: no-cache");
 header("Expires: 0");

ob_clean();
flush();
readfile($zipFile);
unlink($zipFile);
 exit;

when the action is performed, nothing is downloaded and gives krakozyabry. as I understand it the zip file
April 4th 20 at 13:20
3 answers
April 4th 20 at 13:22
Solution
Why nginx raises 500 status when giving the file a script?
Perhaps you are trying to throw the header, when that's not possible
The archive weighs less than 2 gb?
Yes, the files weigh less than 2 gb - mitchel86 commented on April 4th 20 at 13:25
@mitchel86, no errors, no? Before the headers there is no conclusion? - mac_Mayert commented on April 4th 20 at 13:28
@mac_Mayert, to headers no output.
here is my latest code
if($mode === 'exportQrCode')
{
 $search_text = trim($_POST['search_text']);
 $trade_type = $_POST['trade_type'];
 $code = trim($_POST['code']);
 $curator = (int)$_POST['curator'];
 $is_active = $_POST['is_active'];
 $where_array = [];

 if (!empty($curator)) {
 $where = " JOIN SS_B2POS.trade_acc_rel AS tr_acc ON v_tr.idtrade = tr_acc.idtrade".$where." AND (tr_acc.idaccount = ? OR tr_acc.idaccount_second = ?)";
 $where_array[] = $curator;
 $where_array[] = $curator;
}
 if ($trade_type !== "") {
 $where .= "AND idtrade_type=?";
 $where_array[] = $trade_type;
}

 if (strlen($search_text) > 0) {
 $where .= "AND (city LIKE ? OR street LIKE ? OR v_tr.idtrade=? OR brand LIKE ? OR full_name LIKE ? OR message contact_name LIKE ? OR contact_phone LIKE ? OR contact_email LIKE ? OR inet_domain LIKE ? )";
 $where_array[] = "%" . $search_text . "%";
 $where_array[] = "%" . $search_text . "%";
 $where_array[] = $search_text;
 $where_array[] = "%" . $search_text . "%";
 $where_array[] = "%" . $search_text . "%";
 $where_array[] = "%" . $search_text . "%";
 $where_array[] = "%" . $search_text . "%";
 $where_array[] = "%" . $search_text . "%";
 $where_array[] = "%" . $search_text . "%";
}
 if (strlen($code) > 0) {
 $trades_array = [];
 $sth = $dbh->prepare("SELECT idtrade FROM SS_B2POS.bank_trades WHERE bank_trade = ? GROUP BY idtrade ");
$sth->execute([$code]);
 while ($trades_in = $sth->fetch()) {
 $trades_array[] = $trades_in['idtrade'];
}

 if (count($trades_array) == 0) {
 $where .= "AND 2=1";
 } else {
 $trades_in_where = str_repeat("?,", count($trades_array) - 1) . "?";
 $where .= "AND (idtrade IN (" . $trades_in_where . "))";
 $where_array = array_merge($where_array, $trades_array);
}
}
 if ($is_active != ") {
 $where .= 'AND v_tr.md_is_activ = ?';
 $where_array[] = $is_active;
}

 $in_sql = 'SELECT v_tr.idtrade FROM SS_B2POS.v_trade_all v_tr '.$where.

 $sth = $dbh->prepare("SELECT trade_qr_code_file,full_name,city,street,house FROM SS_B2POS.trade_cards trc WHERE trade_qr_code_file IS NOT NULL AND idtrade IN ( ".$in_sql." )");
$sth->execute($where_array);

try{
 $nowDate = date('d.m.Y H:i:s');
 $titleZipFile = 'qrCodesTrades_'.$nowDate.'.zip';
 $zipFile = $pathMedia. '/disk/QRCodes/trade_codes/exportQrCodes/'.$titleZipFile;
 $zip = new ZipArchive;
 if($zip->open($zipFile, ZipArchive::CREATE) !== true){
 throw new Exception("Error!");
}

 while ($code = $sth->fetch()){
 $qrFile = $pathMedia.'/disk/QRCodes/trade_codes/'.substr($code['trade_qr_code_file'], 0, 2).'/'.substr($code['trade_qr_code_file'], 2, 2).'/'.$code['trade_qr_code_file'];

if(file_exists($qrFile)){
 $zip->addFile($qrFile, $code['full_name'].','.$code['city'].','.$code['street'].','.$code['house'].'.png');
}
}
$zip->close();

if(file_exists($zipFile))
{
 header('Content-Description: File Transfer');
 header('Content-Type: application/octet-stream');
 header('Content-Disposition: attachment; filename="' . basename($zipFile).'"');
 header('Content-Transfer-Encoding: binary');
 header('Expires: 0');
 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
 header('Pragma: public');
 header('Content-Length:' . filesize($zipFile));
ob_clean();
flush();

readfile($zipFile);
}
 }catch (Exception $e){
 echo $e->getMessage();
}

 }

the answer gives
PK
d6P:Üuu3ÐÐÑÐµÑ Ð¾ÑÐÑа,Ð3. ÐоÑкÐ2а,10 мн,.pngPNG


IHDR¥¥åPLTEÿÿÿUÂÓ~ pHYsÄÄ+IDATH¥Á¤0rà
¥
n44À
@K1ÑRïgöÇñ.£¼ØùýÛü÷UÃzut^Ú-µjrÑNêðÈJ¥éeª¥rÒ÷?¼úòNå½íMO?]·[^ÆMÏíH_^¤qNüá¦B0k5"þßnÜþþüdýkÊôÊ3jGsú9æïi"ål1ý5^8/4®I?!6Éïí£d§DöI2rY¢°ê.LCצîUÅGQF©R8/áª32ºU>úxê>"÷ÉZ=äEvkxôQâ)íoéFµªñQÎU· ½½ A©\Ë`¡xªXóÅ&òRK{IuÅ{0µYÄjUÚÔyM"ôqV1¼}êw+ÚÔÔn
K^Aç+k;óP¤Ù5×äCHMáñ".JË$ìL:Ü·==ÀA©4ýÔ;!3à¥M. ëEÂ[)Z,¦rºÑGñîdÅʽ4hꦺ¼[±a|2Þ½i÷l±)~Ñç"'5$Ó¡3ºÐüî᩺Ä.Í@oÅwØW\Ì"Áî¡áPXøÔx·`^Ê;ùèl~¡<"UÒE )¿ÍëôÑ°PäCüHÆ5f;§GëJû§MV8fÊå¢ü6VºX§VCz&6ÏDË #>J;§<ºÛêéÊAcؽuÁ¦63µîä bNAó æ¼nû[ùÌCÍlm¤à;ìxuôíµ1êÞ3s9Í©SOÅâ¿&3ýÌ\ßSÄ1ÙÛêNüWÒκ3y3iÍÉKyÙØ"á9ϼuR×/¾u©iÉñ^ù½;QlG͵ó©Rùyûúþ÷õèïÐnDIEND®B'PK
Bd6Pö¼vÐÐ4ÐнÑеÑнеÑ,ÐÑзÑÐ"Ñк,ФÑÑнзе,7Ð.pngPNG


IHDR±±±çÎPLTEÿÿÿUÂÓ~ pHYsÄÄ+pIDATXÁã0EèàÓµáZrHâ|SÜ@|ÓA0÷Ñ3"À%Ìd^
I}}R"ÿé ªá±Õ×æïÉ}c~imç^ÿ}Ü[u2Ly¶óW
IßIÏ(÷Rï)?;ÿù9íËNéçîjOàÍZ:9ù>t_dÿüÊß:pêïé×}iâ<þ^òý<èmÁï§SMî§JõPCmçî(nÝò+íï¾ûg¾òôHu5ÎÑ·qï ó$·ˇ}ÑXvN¾ù®îHSi×$sìàaz#Èè_·ó2ZÀ\nª9Çð¡h uQ=içdJÑÌ
¾ñG®rßjWv0éJ"ó /å:;6ï3ìç°E*0øvÖ$·ÂÈ£Õ01çvîçIîäkÂØWÜ2øÑôÙÌïIFÁc
]í&ä;¿íjA^Rê¥óVNÁý-ñ
U?ûÒÎÃP7Nqé&קKGnUÒÄÿ¥;ø3uð ÅN<Íè|;¿ÜuOó7Ìd?°RÛ96àlïÍZÒ
ä8"tðÏWìÚ,eúQ}l1°8J M:§Æ߶ÜÎI? ká÷®
E|[11Ç+]-Iü8çþ£·Vn½5é7Üà(X%cI§zg+£jøîÞfÈ ¿J ó¥óò"VnµXÓÑWÍvsÈí}Ê3D$Zï¶Bý¥Û3O
óAYkkç~Äíc~2¥°PÃ6?r&:ONm?þÖÎ1êH׶a=ö¾¶sep
n¢E2£.iç16¼ú×}!,Wjås£pânüçzåÛÊiÓõ¶ÑîéGb7µs±{ òjÙl%>ºFüÇðßúvÎ}ÁÙq÷1nÛ9±!$ÅÿÌõ(þVj·ûÃ92ò½
æ.n9µlg>'NéâÈ"ÐJÃ54sòeëÎiIô}áÓÃ-YfE®Ü7uó£}cnçÿéùùµPÚ"IEND®B PK
õc6PÃ1uíÏÏ/ÐазФонР2.1,,ÐеÑеÐ3оÐ2аÑ,33/3.pngPNG


IHDR±±±çÎPLTEÿÿÿUÂÓ~ pHYsÄÄ+oIDATX=í QÐ%@btl)Ù@~6lm ±¤£@ˇ;ÎhôJÐ"ºÅä"£öñ±!úOËL"æ]ù
ùÖiãÚÎ
i-Q=AZ=%Íí|à:Í'î䣧-¤òíêêº1fRÌêauörÒø?ÕÑ#D~ãÐÄ3ľ¿yiâú×Ñé4üôWM|ÕuÕöצÑ"§Ü bçW#q;J>)upò
û$_Ê"Ä°Ús6
P&%èüÖ88ÔÃë:~½Ú5+vpÚJ>Ú½Eê|½=µsuE>8ÎÕefôöâ:IÝÑPjvÈ BWÈ' ¾ "HjçÁÀRÈåÔ)kÕvnr]ÃëìS;å"़YµJÝiç8,x>
?wøãj;7[I[c"ðfæ""tp{u,Z3!b>cvNNªå.T×g)ü"ý'o§ngY|IJ_A

æÏQàPEíáÊÍümõxÄ*\z"6ßBCRzLI¥5sº½øÉêð(¶ã|Ë>ëÆ$ÄCZÔÎe`"3Ø#ß(tÚ9?fV"°½D\öu2ÌàÔÈédhç0AÅÅy,_ñV7×ÑAâ'<íNÔÎÑ5¤×ÏèGVoDÏõðWlÑîðÏåæ.þ8@9ùó)ylåÐ<ºvÅwÔh"µ9'â°$¦ÃÑDnx, aÑ+;861
#ªÀla)µ[rÍrÅuÙóO1ô#4£[KZñHR>íIv^ÊTÜÎÍì±Iu1ã
TÑÁ-C3ya¡cíW/î§Ñáew1LYþ|2ÅdK©,Ú1ܦ6̦ò©"tp1ß
±àhÝ
ü¿vðMN®8)<üóÿ>/R+sú1/tqôiBܤÿ7séVD*G1ÜÍÎs1
`!\ZWµóÿôùÄNäIEND®B PK?
d6P:Üuu3¶ÐÐÑÐµÑ Ð¾ÑÐÑа,Ð3. ÐоÑкÐ2а,10 мн,.pngPK?
Bd6Pö¼vÐÐ4¶ÆÐнÑеÑнеÑ,ÐÑзÑÐ"Ñк,ФÑÑнзе,7Ð.pngPK?
õc6PÃ1uíÏÏ/¶èÐазФонР2.1,,ÐеÑеÐ3оÐ2аÑ,33/3.pngPK - mitchel86 commented on April 4th 20 at 13:31
@mitchel86, headers are not set. Maybe there where a space or BOM signature. There - mac_Mayert commented on April 4th 20 at 13:34
@mitchel86, well, you clean up the buffer after header
ob_clean();
Well, resetting the buffer after reading it - mac_Mayert commented on April 4th 20 at 13:37
@mac_Mayert, unfortunately nothing helps - mitchel86 commented on April 4th 20 at 13:40
@mitchel86, try to debug on a small file without heavy functions zip. There is some sort of a trifle, take the code that I threw, and in a separate file a try. Well, and add the path to the file. Also try another abuser, or another server, may be some nuances - mac_Mayert commented on April 4th 20 at 13:43
April 4th 20 at 13:24
Solution
if (ob_get_level()) {
ob_end_clean();
}
 header('Content-Description: File Transfer');
 header('Content-Type: application/octet-stream');
 header('Content-Disposition: attachment; filename=' . basename($zipFile));
 header('Content-Transfer-Encoding: binary');
 header('Expires: 0');
 header('Cache-Control: must-revalidate');
 header('Pragma: public');
 header('Content-Length:' . filesize($zipFile));
readfile($zipFile);
 exit;
April 4th 20 at 13:26
Solution
If you just want to send a file, and you have nginx, it is better to use the header X-Accel-Redirect to nginx request this file to give.

Find more questions by tags PHP