Solved

Download files from server in zip format.

Posted on 2013-06-27
5
571 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 108

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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 look for a specific file type in a local or remote server directory using PHP.

914 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now