Question

I'm getting a weird error while trying to open a PHP, created .zip file in windows explorer.

It works fine in WinRar.

What I am creating is a SDS sheet download site where you can checkbox what kinds of PDF documents you want and then zip them.

The procedure works fine and I have no errors with the file creation. Its only when opening with windows explorer the error occurs.

Error message: "Windows cannot open the folder. The Compressed (zipped) Folder 'filename' is invalid." error opening in Windows Explorer."

The site is http://www.premiere-produkter.no/pp/datablad/index.php if you want to test it out yourselves.

Heres the code:

<?php
    $error = "";        //error holder
    if(isset($_POST['createpdf'])){
        $post = $_POST;     
        $file_folder = "files/";    // folder to load files
        if(extension_loaded('zip')){    // Checking ZIP extension is available
            if(isset($post['files']) and count($post['files']) > 0){    // Checking files are selected
                $zip = new ZipArchive();            // Load zip library 
                $zip_name = time().".zip";          // Zip name
                if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE){       // Opening zip file to load files
                    $error .=  "* Sorry ZIP creation failed at this time<br/>";
                }
                foreach($post['files'] as $file){               
                    $zip->addFile($file_folder.$file);          // Adding files into zip
                }
                $zip->close();
                if(file_exists($zip_name)){
                    // push to download the zip
                    header('Content-type: application/zip');
                    header('Content-Disposition: attachment; filename="'.$zip_name.'"');
                    readfile($zip_name);
                    // remove zip file is exists in temp path
                    unlink($zip_name);
                }

            }else
                $error .= "* Please select file to zip <br/>";
        }else
            $error .= "* You 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 her </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å for å 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>
Was it helpful?

Solution 2

I'll elaborate my comment. Your ZIP file contains valid ZIP stuff plus a complete HTML document at the end. And I don't mean you've added properly compressed HTML at the end—it's simply appended:

ZIP+HTML

There's no way to say how this happened from the code you've shared but it should be fairly obvious to fix once you've aware of it.


Educate guess - I suppose you have this:

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

You can for instance finish the script before running the unwanted code:

unlink($zip_name);
exit;

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

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

OTHER TIPS

add

ob_clean();
ob_end_flush();

before

header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);

Notice: Add this code to clean all html out put before send zip content to browser

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top