Link to home
Start Free TrialLog in
Avatar of jblayney
jblayneyFlag for Canada

asked on

save my file as zip

Hello,

I am trying to write a backup script for my database, it is working pretty good, makes a .sql copy and saves to the server. I would like to save it as a zip archive instead. This is my code to save as .sql (I took out all my zip experiments)

        $now = date("Y-M-d-H");
	$handle = fopen('db-backup-'.$now.'.sql','w+');
	fwrite($handle,$return);
	fclose($handle);

Open in new window

Avatar of Pavel Celba
Pavel Celba
Flag of Czechia image

You may try this code:
<?php

$zip = new ZipArchive();
$filename = "./ZIPname.zip";

if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");
}

$localfilename = 'db-backup-'.$now.'.sql'

$zip->addFile($localfilename);
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
?>

Open in new window

Of course, you have to provide correct path names.
More info: http://php.net/manual/en/ziparchive.addfile.php
http://php.net/manual/en/zip.examples.php
PHP ZipArchive has something of a checkered past.  Not everything has always worked perfectly, so be sure to make your tests carefully before you develop a dependency on it!

Here's my teaching example showing how it's done.
<?php // demo/zip_archive.php
/**
 * Create a Zip Archive from a subdirectory
 *
 * https://www.experts-exchange.com/questions/29038857/save-my-file-as-zip.html
 *
 * http://php.net/manual/en/zip.examples.php       <-- No matter when errors "occur," they show up at Zip::Close() time.
 * http://php.net/manual/en/zip.examples.php#85714 <-- Errors are obscure!
 * http://php.net/manual/en/class.ziparchive.php
 * http://php.net/manual/en/ziparchive.close.php#101606
 */
error_reporting(E_ALL);


// THIS IS THE LIST OF scandir() RESPONSES THAT WE DO NOT WANT
$unwanted
= array
( '.'
, '..'
, 'teste2.txt'
)
;


// THIS IS THE NAME OF THE DIRECTORY TO ZIP
$dir = 'oop5';


// A GOOD PATH ON MY SERVER
$path
= getcwd()
. DIRECTORY_SEPARATOR
. $dir
;
if (!is_dir($path)) trigger_error("FAIL: NOT VALID PATH $path", E_USER_ERROR);


// 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) trigger_error("FAIL: ZIP->OPEN $archive", E_USER_ERROR);


// GET THE LIST OF FILES
if (!$files = scandir($path)) trigger_error("FAIL: scandir() $path", E_USER_ERROR);

foreach ($files as $file)
{
    $filename = $path . DIRECTORY_SEPARATOR . $file;

    // SKIP THE UNWANTED AND ADD THE OTHERS TO THE ARCHIVE
    if ( in_array($file, $unwanted) ) continue;
    if ( !is_file($filename) )        continue;
    // ACTIVATE THIS FOR A PROGRESS REPORT:
    // echo PHP_EOL . "$filename: $file";
    $zip->addFile($filename, $file );
}

if (!$zip->close()) trigger_error("FAIL: ZIP->CLOSE()", E_USER_ERROR);


$fs  = filesize($archive);
$fsn = number_format($fs);


// PREPARE A LINK
$link
= '<a target="_blank" href="'
. $archive
. '">'
. "DOWNLOAD $archive $fsn BYTES"
. '</a>'
;
echo PHP_EOL . $link;


/**
 * You can use something like this to send the Zip Archive directly to the browser
 *
header("Content-Type: archive/zip");
header("Content-Disposition: attachment; filename=$dir".".zip");
header("Content-Length: $fs");
readfile($archive);
 */

Open in new window

just add this

$now = date("Y-M-d-H");
$filename = 'db-backup-'.$now.'.sql';

$handle = fopen($filename,'w+');
fwrite($handle,$return);
fclose($handle);

$zip = new ZipArchive();
$zipfile = 'db-backup-'.$now.'.zip';
$zip->addFile($zipfile, $filename);
$zip->close();

Open in new window

Avatar of jblayney

ASKER

Hello,

Thanks for all the comments, no ones code works though, it is very odd.

no errors, no zip created..
this is my code now

$now = date("Y-M-d-H");
	$handling = fopen('db-backup-'.$now.'.sql','w+');
	fwrite($handling,$return);
	fclose($handling);
	


$zip = new ZipArchive();
$zipfile = 'db-backup-'.$now.'.sql.zip';
$zip->addFile($zipfile, $handling);
$zip->close();

Open in new window

pcelba and ray, in both your demos it seem like a i need an existing directory, is that the case?
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thank you