Solved

PHP Zip

Posted on 2013-05-15
7
426 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
[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
  • 2
7 Comments
 
LVL 110

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 110

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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 110

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 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