PHP .zip file download error when opening in windows explorer -


i'm getting weird error while trying open php, created .zip file in windows explorer.

it works fine in winrar.

what creating sds sheet download site can checkbox kinds of pdf documents want , zip them.

the procedure works fine , have no errors file creation. when opening windows explorer error occurs.

error message: "windows cannot open folder. compressed (zipped) folder 'filename' invalid." error opening in windows explorer."

the site http://www.premiere-produkter.no/pp/datablad/index.php if want test out yourselves.

heres code:

<?php     $error = "";        //error holder     if(isset($_post['createpdf'])){         $post = $_post;              $file_folder = "files/";    // folder load files         if(extension_loaded('zip')){    // checking zip extension available             if(isset($post['files']) , count($post['files']) > 0){    // checking files selected                 $zip = new ziparchive();            // load zip library                  $zip_name = time().".zip";          // zip name                 if($zip->open($zip_name, ziparchive::create)!==true){       // opening zip file load files                     $error .=  "* sorry zip creation failed @ time<br/>";                 }                 foreach($post['files'] $file){                                    $zip->addfile($file_folder.$file);          // adding files zip                 }                 $zip->close();                 if(file_exists($zip_name)){                     // push download zip                     header('content-type: application/zip');                     header('content-disposition: attachment; filename="'.$zip_name.'"');                     readfile($zip_name);                     // remove zip file exists in temp path                     unlink($zip_name);                 }              }else                 $error .= "* please select file zip <br/>";         }else             $error .= "* dont have zip extension<br/>";     } ?> 

html:

    <body>         <div id="container">     <div id="meny">     <h2><a href="http://www.premiere-produkter.no"><img src="files/pp.png">gå til premiere produkter sine sider ved å klikke </a> </h2>      <h1>last ned datablad / download sds sheets</h1>     <form name="zips" method="post">     <?php if(!empty($error)) { ?>     <p style=" border:#c10000 1px solid; background-color:#ffa8a8; color:#b00000;padding:8px; margin:0 auto 10px;"><?php echo $error; ?></p>     <?php } ?>     <h5>     <p>vennligst marker de databladene du ønsker å få tak i, og så trykk på "last ned til zip" etterpå å laste ned en komprimert zip fil der alle databladene ligger i. </p>     <p>du kan også se på pdf filene ved å trykke på navnet.</p>      <p>sliter du med å finne produktet? bruk søkefeltet til å søke etter enten navn eller produktnummer.     </h5>     <table class="tablesorter" id="tblsearch" width="600" border="1" align="center" cellpadding="10" cellspacing="0" style="border-collapse:collapse; border:#ccc 1px solid; background:#fff;">       <thead>       <tr>         <th  align="center"><span style="font-size:13px;">marker de du vil laste ned</span></th>         <th align="center">file type</th>         <th>fil navn</th>         <th>språk</th>         <th>faresymboler</th>         <th>art.nr</th>       </tr>       </thead>          <tr>         <td align="center"><input type="checkbox" name="files[]" value="11001-11081.pdf" /></td>         <td align="center"><img src="files/pdf.png" title="pdf" width="16" height="16" /></td>         <td><a href="files/11001-11081.pdf">savona d2</a></td>         <td align="center" alt="nok"><span style="font-size:0px;">no</span><img src="norge.jpg" alt="nor" title="nor" width="20" height="20" /></td>         <td align="center"><img src="/images/faresymboler/irriterende.png" title="pdf" width="20" height="20" /></td>         <td>11001-11081</td>       </tr>     </table>  <div id="buttons"> <div class="b1"> <input type="submit" value="last ned som zip fil" style="border:0px; margin:10px 0;   background-color:#800040; color:#fff; padding:7px; width:234px; cursor:pointer; font-  weight:bold; border-radius:0px;" name="createpdf"> </div> <div class="b1"> <input type="reset" value="reset" style="border:0px; background-color:#d3d3d3; color:#000;   font-weight:bold; width:235px; padding:10px; cursor:pointer; border-radius:0px;"   name="reset"> </div> 

i'll elaborate comment. zip file contains valid zip stuff plus complete html document @ end. , don't mean you've added compressed html @ end—it's appended:

zip+html

there's no way how happened code you've shared should obvious fix once you've aware of it.


educate guess - suppose have this:

if(file_exists($zip_name)){     // push download zip     header('content-type: application/zip');     header('content-disposition: attachment; filename="'.$zip_name.'"');     readfile($zip_name);     // remove zip file exists in temp path     unlink($zip_name); } ?> <!doctype html> <head> ... 

you can instance finish script before running unwanted code:

unlink($zip_name); exit; 

... or use if() constructs not run it:

if(file_exists($zip_name)){     // ... }else{ ?>     <!doctype html>     <head> <? } 

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -