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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

PHP Zip

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
john_yourspace
Asked:
john_yourspace
  • 3
  • 2
  • 2
2 Solutions
 
Ray PaseurCommented:
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
 
john_yourspaceAuthor Commented:
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
 
Ray PaseurCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
GaryCommented:
Are you not saying if this is not a file continue
if ( !is_file($path . DIRECTORY_SEPARATOR . $file) ) continue;
0
 
Ray PaseurCommented:
@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
 
GaryCommented:
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
 
john_yourspaceAuthor Commented:
Thanks for all your help guys got sorted

John
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now