Forum Moderators: coopster

Message Too Old, No Replies

Easiest way to zip a folder and all contents for download?

         

mr_nabo

11:47 am on Nov 18, 2008 (gmt 0)

10+ Year Member



Hi,

I realise that the following code lets you unzip the contents of a zip file in the same directory:

<?php system('unzip zipFileName.zip'); ?>

But is there an easy way to do the reverse? It's so that I can zip up my website public_html folder and download it easily.

I tried this, but it didn't work:

<?php system('zip zipFolderName'); ?>

Cheers

coopster

12:16 pm on Nov 18, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



but it didn't work

Can you be a bit more descriptive? Did it return anything at all? Did it fail? Have you checked your error messages/logs? Is it a permissions issue?

mr_nabo

12:41 pm on Nov 18, 2008 (gmt 0)

10+ Year Member



Hi Coopster,

Sorry, yes of course. It literally doesn't do anything when I run the script - I tried putting in this code to see if it was working (I assume this is the way to do it, sorry I'm still learning):

<?php 

if (system('zip zipmeup.zip')) {

echo 'Zipped';

} else {

echo 'failed';

}

?>

And it says 'failed', so I assume it's not running. Do I need to change directory or something for this to work?

Thanks for getting back to me

coopster

12:56 pm on Nov 18, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK. I'm guessing you are likely running into a permissions issue, if anything. However, to recursively compress a directory into your archive you would need the -R switch in your command.

I'm assuming you are doing this for yourself? Meaning, you don't have command line access to your server and this is something of a backup you are creating for yourself? If so, most hosts provide some form of backup option when they do not allow you command line access. You may want to check in your control panel.

mr_nabo

1:32 pm on Nov 18, 2008 (gmt 0)

10+ Year Member



Thanks for your help Coopster. I thought I'd look around and try some things out. I put together this code which works perfectly for what I need:

Cheers,


<?php
// Set path to download zip file
$dl_url = 'http://www.domainname.com/_backup/filename.zip';

// Check if everything's cool and groovy - create a link to download zip file
if (system('zip -r /home/path/domain/_backup/filename.zip /home/path/domain'))
{
echo '<p>All zipped up!</p>';
echo '<a href="' . $dl_url . '">Download</a>';
} else {
echo '<p>Balls! Didn\'t work.</p>';
}

?>