Solved

PHP Zip

Posted on 2013-05-15
7
416 Views
Last Modified: 2013-05-15
Hi guys,

I am trying to zip an entire folder with the below code,

seems to just create an archive with no folders etc just a htaccess file

Any suggestions

this is wordpress and needs to zip the entire uploads dir


//create zip

$dir = 'wp-content/uploads/';
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");

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

// CREATE A DATE-TIME ARCHIVE NAME
$archive='dump' . DIRECTORY_SEPARATOR. date('Ymd\THis'). '_archive_.zip';
var_dump($archive);
// TRY TO CREATE THE ARCHIVE
if ($zip->open($archive, ZIPARCHIVE::CREATE)!==TRUE) die("FAIL: ZIP->OPEN <$archive>");

// GET THE LIST OF FILES
foreach (glob($dir."*") as $file) 
{
    // SKIP THE UNWANTED AND ADD THE OTHERS TO THE ARCHIVE
    if ( !is_file($path . DIRECTORY_SEPARATOR . $file) ) continue;
    $zip->addFile($path . DIRECTORY_SEPARATOR . $file );
}

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

$localZIP = getcwd(). DIRECTORY_SEPARATOR.$archive;

Open in new window

0
Comment
Question by:john_yourspace
  • 3
  • 2
  • 2
7 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39167858
The WordPress part of things may get in the way.  Suggest you create the SSCCE with a simple directory of one or two small text files, outside of WordPress, and test there.  Then if that works, try the script again using the wp-content.
0
 

Author Comment

by:john_yourspace
ID: 39167921
thanks Ray,

I am running it from an external file so its self contained already - its merely a wordpress directory i want to zip

J
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 200 total points
ID: 39168154
The ZipArchive class is a bit flaky.  You might want to look carefully at the error codes it can generate.  Also consider the contents of the directory.  Large files?  Empty files?  IIRC things like that can trip the ZipArchive class.  I don't have time to research it right now but I have seen a couple of user-contributed notes related to empty output archives.
http://php.net/manual/en/class.ziparchive.php
http://php.net/manual/en/ref.zip.php

I have also tested this script and verified that it works correctly.  However Windows 7 seems to have trouble finding the directory tree inside the Zip Archive.
<?php // RAY_zip_archive_test.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;

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 58

Expert Comment

by:Gary
ID: 39168256
Are you not saying if this is not a file continue
if ( !is_file($path . DIRECTORY_SEPARATOR . $file) ) continue;
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39168270
@GaryC123: Yes.  The script does not add things like sub-directories or the "." and ".." pointers to the archive.

I think the problem is that after you create the archive and look at it with Windows Explorer, Windows says it has zero files.  That's not true.  When I unzipped the archive, it produced a directory tree in the output folder.  Once I navigated down the directory tree I eventually got to a directory that had all of the files!
0
 
LVL 58

Accepted Solution

by:
Gary earned 300 total points
ID: 39168339
Try this

<?php
//create zip

$dir = 'wp-content/uploads/';
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");

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

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

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

// GET THE LIST OF FILES
$directory = new RecursiveDirectoryIterator($dir);
foreach (new RecursiveIteratorIterator($directory) as $filename => $file) {
    if ( !is_file($filename) ) continue;
    $zip->addFile($filename);

}

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

$localZIP = getcwd(). DIRECTORY_SEPARATOR.$archive; 

Open in new window

0
 

Author Closing Comment

by:john_yourspace
ID: 39168769
Thanks for all your help guys got sorted

John
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

WordPress can be pretty daunting, especially for a beginner, so I thought it might be a good idea to write an article to show how easy it is to get started in WordPress and to design a custom theme.  The first step is to check with your hosting comp…
WordPress is constantly evolving, and with each evolution appears to get better and better.  One of the big drawbacks prior to version 3 was that there was no way to be able to set up a custom menu from the backend. The Old Way Adding menus is…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now