Hi Vivek, can I ask why you aren't using the [man]ZIPArchive[/man] class? I didn't look at the class you're using but with the ZIPArchive class you can tell the archive what path the files should be added as. I'm on my way out the door to work but I'll post my back-up script which zips my site and hopefully that will help you out, it also backs up my DB and then FTPs the zip to another server but you can just ignore that stuff:
<?php
// Settings
$flags = array(
'DB' => TRUE,
'FTP' => TRUE,
);
// Directory
$folderBlacklist = array('cgi-bin');
$dir = '/home/user/public_html';
// Database
$DBHOST = '---';
$DBUSER = '---';
$DBPASS = '---';
$DBNAME = array('db1','db2');
// FTP
$FTPHOST = '----';
$FTPUSER = '----';
$FTPPASS = '----';
// PREVIOUS VALUES OBFUSCATED FOR PUBLIC FORUM
/********************************************\
**** Don't Edit Below HERE unless broken ***
\********************************************/
// MySQL DUMP
if( $flags['DB'] ) {
if( is_array($DBNAME) ) {
foreach( $DBNAME as $NAME ) {
dumpDB($NAME);
}
} else {
dumpDB($DBNAME);
}
}
// Initialize zip file
$zipname = date('Y-m-d').'_'.$_SERVER['SERVER_NAME'].'.zip';
if( file_exists($zipname) ) unlink($zipname);
$zip = new ZipArchive();
$zip->open($zipname,ZipArchive::CREATE);
addToZip($dir,$zip);
if( $zip->close() === FALSE ) {
print("\nArchive creation failed.\n");
} else {
print("\nArchive creation successful.\n");
}
if( $flags['FTP'] ) {
if( $ftp = ftp_connect($FTPHOST) ) {
if( ftp_login($ftp,$FTPUSER,$FTPPASS) ) {
ftp_pasv($ftp,TRUE);
if( ftp_put($ftp,$zipname,basename($zipname),FTP_BINARY) ) {
echo "\n\nFile Upload successful.";
unlink($zipname);
} else {
echo "\n\nFile Upload failed.";
}
} else {
echo "\n\nFTP login failed.";
}
ftp_close($ftp);
} else {
echo "\n\nFTP connection failed.";
}
}
function dumpDB($db) {
global $DBHOST,$DBUSER,$DBPASS;
$sql = 'DATABASE_'.$db.'.sql';
if( file_exists($sql) ) unlink($sql);
$cmd = "mysqldump --opt -h {$DBHOST} -u {$DBUSER} -p{$DBPASS} {$db} >{$sql}";
shell_exec($cmd);
}
function addToZip($path,ZipArchive $zip) {
static $depth = 0;
static $root;
if( $depth == 0 && is_dir($path) ) {
$root = realpath($path) . DIRECTORY_SEPARATOR;
$depth++;
}
if( is_file($path) ) {
$file = str_replace($root,'',$path);
if( $zip->addFile($path,$file) ) {
echo "Added $path as $file\n";
} else {
echo "FAILED $path\n";
}
} elseif( is_dir($path) ) {
echo "\nAdding folder: $path\n";
$dir = realpath($path) . DIRECTORY_SEPARATOR;
$dir = glob($dir.'*');
array_walk($dir,'filterGlob');
$dir = array_filter($dir);
usort($dir,'sortGlob');
foreach( $dir as $sub ) {
addToZip($sub,$zip);
}
} else {
return FALSE;
}
}
function sortGlob($a,$b) {
if( is_dir($a) && is_file($b) ) return -1;
if( is_file($a) && is_dir($b) ) return 1;
return strcasecmp($a,$b);
}
function filterGlob(&$path,$key) {
GLOBAL $folderBlacklist;
if( in_array(basename($path),$folderBlacklist) )
$path = FALSE;
}