Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP ZipArchive() creates corrupt zip file

Posted on 2012-03-22
5
Medium Priority
?
835 Views
Last Modified: 2014-05-03
I have upgraded the server and since found that my zip is no longer working.

I currently am using: Apache 2.2.22 , PHP 5.3.10

Configure Command
'./configure' '--disable-fileinfo' '--disable-pdo' '--enable-bcmath' '--enable-calendar' '--enable-ftp' '--enable-gd-native-ttf' '--enable-libxml' '--enable-magic-quotes' '--enable-mbstring' '--enable-sockets' '--enable-zip' '--prefix=/usr' '--with-curl=/opt/curlssl/' '--with-curlwrappers' '--with-freetype-dir=/usr' '--with-gd' '--with-gettext' '--with-imap=/opt/php_with_imap_client/' '--with-imap-ssl=/usr' '--with-jpeg-dir=/usr' '--with-kerberos' '--with-libdir=lib64' '--with-libxml-dir=/opt/xml2/' '--with-mcrypt=/opt/libmcrypt/' '--with-mysql=/usr' '--with-mysql-sock=/var/lib/mysql/mysql.sock' '--with-openssl=/usr' '--with-openssl-dir=/usr' '--with-pcre-regex=/opt/pcre' '--with-pic' '--with-png-dir=/usr' '--with-xpm-dir=/usr' '--with-zlib' '--with-zlib-dir=/usr'


A simple script:

eg:
$zip = new ZipArchive();
$filename = "test112.zip";

if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");
}

$zip->addFile("Lighthouse.jpg","file1.jpg");
$zip->addFile("Penguins.jpg","file2.jpg");

echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();

------------
(using all: Windows 7 zip, WinZip, WinRAR)

The zip file opens and the filesize looks ok in the two files, but when I open the images, it says they are corrupt.

I have tried both ZipArchive and also pclZipLib... both work with the same results.

Thanks
0
Comment
Question by:amakaram
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37755004
I am also using PHP 5.3.10.  Here is my ZIP information:
Zip       enabled
Extension Version       $Id: php_zip.c 321634 2012-01-01 13:15:04Z felipe $
Zip version       1.9.1
Libzip version       0.9.0

If you will post the example files here I will be glad to try them on my server.
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 37755047
Using this code, I successfully Zipped up a directory full of GIF images.  Windows 7 could not open the Zip Archive, but it worked fine with WinRAR.
<?php // RAY_zip_archive.php
error_reporting(E_ALL);
date_default_timezone_set('America/New_York');

// THE URL ARGUMENT IS THE NAME OF THE DIRECTORY TO ZIP
$dir = (!empty($_GET["dir"])) ? $_GET["dir"] : NULL;

// BUT FOR MY TESTS IT IS HARDCODED
$dir = 'RAY_aslimages';

if (!$dir) die("PLEASE PROVIDE dir= IN THE URL");

// A GOOD PATH ON MY SERVER
$path
= getcwd()
. DIRECTORY_SEPARATOR
. $dir
;
if (!is_dir($path)) die("FAIL: NOT VALID PATH $path");

// THIS IS THE LIST OF scandir() RESPONSES THAT WE DO NOT WANT
$unwanted
= array
( '.'
, '..'
, 'teste2.txt'
)
;

// INSTANTIATE THE OBJECT
$zip = new ZipArchive();

// CREATE A DATE-TIME ARCHIVE NAME
$archive
= date('Ymd\THis')
. '_archive_'
. $dir
. '.zip'
;

// TRY TO CREATE THE ARCHIVE
if ($zip->open($archive, ZIPARCHIVE::CREATE)!==TRUE) die("FAIL: ZIP->OPEN <$archive>");

// GET THE LIST OF FILES
if (!$files = scandir($path))   die("FAIL: scandir() $path");
foreach ($files as $file)
{
    // SKIP THE UNWANTED AND ADD THE OTHERS TO THE ARCHIVE
    if ( in_array($file, $unwanted) )                    continue;
    if ( !is_file($path . DIRECTORY_SEPARATOR . $file) ) continue;
    $zip->addFile($path . DIRECTORY_SEPARATOR . $file );
}

if (!$zip->close()) die("FAIL: ZIP->CLOSE");

$fs  = filesize($archive);
$fsn = number_format($fs);


// PREPARE A LINK
$link
= '<a target="_blank" href="'
. $archive
. '">'
. "DOWNLOAD $archive $fsn BYTES"
. '</a>'
;
// echo $link;

// SEND THE ZIP ARCHIVE
header("Content-Type: archive/zip");
header("Content-Disposition: attachment; filename=$dir".".zip");
header("Content-Length: $fs");
readfile($archive);

Open in new window

0
 

Author Comment

by:amakaram
ID: 37756851
Well Ray_Paseur, I have good news...

As I was getting the script and the images I was testing with, this EE uploader was failing them... upon closer look, the images were corrupt on the server... they originally were ftp'd from one server to the other and I guess I did not check the originals. (cant really believe that, but have no proof to the contrary)

Of course this was a small clip from a larger sript and I went back to the section of the script that moves the files in the first place... updated FTP_ASCII with FTI_BINARY and it worked.

...(ftp_fget($conn_id, $handle, $FilesMyrow["CurrentFileName"], FTP_BINARY, 0)...

Again, not sure why this would change on the new server... they have been ASCII for the last 2 years.

Let me know what you would like to do with points...

Thanks for your help!
Lighthouse.jpg
Penguins.jpg
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 37756941
Glad to hear that.  I tested with your image files and got the same result as before - WinRaR did a fine job.

Since it's not a ZIP related issue, I suggest you delete the question.  Not worried about the points - I have enough points to orbit Saturn.

All the best, ~Ray
0
 

Author Comment

by:amakaram
ID: 37757057
Well thank you.

Your responses were correct... but the problem *embarrassed smile* was mine!

Will delete shortly.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question