Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP ZipArchive() creates corrupt zip file

Posted on 2012-03-22
5
Medium Priority
?
885 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
  • 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

927 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