?
Solved

Download files from server in zip format.

Posted on 2013-06-27
5
Medium Priority
?
578 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 111

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 1500 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

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

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

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Suggested Courses

718 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