Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP Zip

Posted on 2013-05-15
7
Medium Priority
?
429 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 111

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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 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
Plesk WordPress Toolkit

Plesk's WordPress Toolkit allows server administrators, resellers and customers to manage their WordPress instances, enabling a variety of development workflows for WordPress admins of all skill levels, from beginners to pros.

See why 2/3 of Plesk servers use it.

 
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 111

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 1200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses four methods for overlaying images in a container on a web page
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

704 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