Solved

Download files from server in zip format.

Posted on 2013-06-27
5
577 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 110

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

630 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