Solved

Download files from server in zip format.

Posted on 2013-06-27
5
572 Views
Last Modified: 2013-07-01
Hi Experts!

I'm working with Wamp localhost and I use simple script to download files directly from ftp server:
<?php
/* ftp login details here: ftp_login()... */
$source = 'httpdocs/MARINER_DATA/2013 xml/WK_10/dir;
$local = 'httpdocs/MARINER_DATA/2013 xml/WK_10/dir/;
$content = ftp_nlist($connection, $source);
foreach ($content as $onefile) {
      $destination = $local.$onefile;
      if (ftp_get($connection, $destination, $onefile, FTP_BINARY)) {
            //success
      }
}
?>
Pretty simple. But instead downloading files one by one, I want to create one zip file on server and then download it to localhost. I found nice script allowing you to create zip files based on filepaths from array: http://davidwalsh.name/create-zip-php
It works fine if I run it on localhost and it builds zip file from files on localhost or: run it on online server and build zip file from files on the same online server. But what I want is to request the files from localhost (just send list of files I want in array), create zip file online and then download it from online location to location in localhost, I hope that makes sense to you? It's the only problem and I'd like you to help me with this if possible.

I appreciate your help.
Thanks.
0
Comment
Question by:Zado
  • 2
  • 2
5 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39281680
Here is what I have used.  Maybe you can tailor it for your needs.

<?php // RAY_zip_archive.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;

// SEND THE ZIP ARCHIVE
header("Content-Type: archive/zip");
header("Content-Disposition: attachment; filename=$dir".".zip");
header("Content-Length: $fs");
readfile($archive);

Open in new window

HTH, ~Ray
0
 
LVL 16

Expert Comment

by:AlexPace
ID: 39282180
Are you saying that you want to send the FTP server a list of files and then have the server create a zip file which you then subsequently download?  If so, why not just use an FTP server that does compression on the fly?
0
 
LVL 8

Author Comment

by:Zado
ID: 39282193
"Are you saying that you want to send the FTP server a list of files and then have the server create a zip file which you then subsequently download?"
Yep, that's exactly what I want.

"If so, why not just use an FTP server that does compression on the fly?"
I'm not sure how to do it/where to start, could you give me some hint, please?

Thanks
0
 
LVL 16

Accepted Solution

by:
AlexPace earned 500 total points
ID: 39282258
Send the FEAT verb when you log in and the server should reply with a list of supported commands... look in the list for MODE Z.
0
 
LVL 8

Author Closing Comment

by:Zado
ID: 39289491
Thanks.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with query 3 26
URGENT - Wordpress site missing  theme 9 41
google analytics in wordpress 4 35
PHP not parsing ' character 12 40
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

803 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