PHP .zip file download error when opening in windows explorer

Tommy

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>
Álvaro González

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>
<? }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP .zip file download error when opening in windows explorer

From Dev

PHP created ZIP file not working with Windows Explorer

From Dev

Error when knitr has to download a zip file

From Dev

ZipException: error in opening zip file

From Dev

PHP file opening error

From Dev

PHP file opening error

From Dev

ZIP archives not opening in Explorer on some Windows 10 machines

From Dev

Error when opening saved file

From Dev

PHP File Download Error

From Dev

How should the temporary file be saved when creating and download zip file using PHP?

From Dev

PHP header attachment download corrupts ZIP file

From Dev

Download a zip file from PHP readfile() in Java

From Dev

create and download zip file using php

From Dev

create and download zip file using php

From Dev

curl php response 204 to download zip file

From Dev

PHP: cURL download ZIP file - smaller filesize

From Dev

Download multiple .txt files into .zip file with PHP

From Dev

PHP download multiple files as zip error

From Dev

Android Studio 0.8.7: Error:Cause: error in opening zip file

From Dev

Error opening zip file or JAR manifest missing : jrebel.jar

From Dev

Error opening zip file or JAR manifest missing : C:\Program

From Dev

Android studio - Failed to complete gradle execution - error in opening zip file

From Dev

Cordova Build Gradle Error while opening/extracting zip file

From Dev

Windows Phone 8 UnauthorizedAccessException when opening file

From Dev

Windows Phone 8 UnauthorizedAccessException when opening file

From Dev

PHP file download strange error

From Dev

Windows Explorer/Control Panel not opening

From Dev

Encoding error when opening an Excel file with xlrd

From Dev

Emacs polymode gives error when opening file

Related Related

  1. 1

    PHP .zip file download error when opening in windows explorer

  2. 2

    PHP created ZIP file not working with Windows Explorer

  3. 3

    Error when knitr has to download a zip file

  4. 4

    ZipException: error in opening zip file

  5. 5

    PHP file opening error

  6. 6

    PHP file opening error

  7. 7

    ZIP archives not opening in Explorer on some Windows 10 machines

  8. 8

    Error when opening saved file

  9. 9

    PHP File Download Error

  10. 10

    How should the temporary file be saved when creating and download zip file using PHP?

  11. 11

    PHP header attachment download corrupts ZIP file

  12. 12

    Download a zip file from PHP readfile() in Java

  13. 13

    create and download zip file using php

  14. 14

    create and download zip file using php

  15. 15

    curl php response 204 to download zip file

  16. 16

    PHP: cURL download ZIP file - smaller filesize

  17. 17

    Download multiple .txt files into .zip file with PHP

  18. 18

    PHP download multiple files as zip error

  19. 19

    Android Studio 0.8.7: Error:Cause: error in opening zip file

  20. 20

    Error opening zip file or JAR manifest missing : jrebel.jar

  21. 21

    Error opening zip file or JAR manifest missing : C:\Program

  22. 22

    Android studio - Failed to complete gradle execution - error in opening zip file

  23. 23

    Cordova Build Gradle Error while opening/extracting zip file

  24. 24

    Windows Phone 8 UnauthorizedAccessException when opening file

  25. 25

    Windows Phone 8 UnauthorizedAccessException when opening file

  26. 26

    PHP file download strange error

  27. 27

    Windows Explorer/Control Panel not opening

  28. 28

    Encoding error when opening an Excel file with xlrd

  29. 29

    Emacs polymode gives error when opening file

HotTag

Archive