Solved

PHP Zip

Posted on 2013-05-15
7
420 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 109

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 109

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 109

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

Gigs: Get Your Project Delivered by an Expert

Select from 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

Suggested Solutions

Title # Comments Views Activity
What is the WordPress Exit hook? 1 23
Really simple no curl. Send a post 3 times 4 24
PHP curl issue VERBOSE output 18 34
How can I make this form submit to itself? 10 25
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…
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
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 purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…

808 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