For the redirection part, it's a poor idea to try and call the same module using both method and function calling. One or the other really needs to be chosen.

Option 1: Make it all function calls by importing redirect, as well.

#!/usr/bin/perl -w #use strict; use Archive::Zip qw(:ERROR_CODES :CONSTANTS); use CGI qw(:standard :nph redirect); my $id; if ($id = param('link')){ my $obj = Archive::Zip->new(); my $file_member = $obj->addFile( $id ); if ($obj->writeToFileNamed('download.zip') != AZ_OK) { print header(type => 'text/plain'); print "Bad something or other!"; } else { print redirect('http://domain.com/files/download.zip'); } }

Option 2: Make it all method calls, by not importing any functions.

#!/usr/bin/perl -w #use strict; use Archive::Zip qw(:ERROR_CODES :CONSTANTS); use CGI qw(:nph); my $q = CGI->new(); my $id; if ($id = $q->param('link')){ my $obj = Archive::Zip->new(); my $file_member = $obj->addFile( $id ); if ($obj->writeToFileNamed('download.zip') != AZ_OK) { print $q->header(type => 'text/plain'); print "Bad something or other!"; } else { print $q->redirect('http://domain.com/files/download.zip'); } }

I've also done three other things:

  1. I made it a non-parsed-headers (nph) script. Otherwise, the first thing that is send by Apache (or other web servers) to your browser is a 200 OK header, as far as I can recall, and the redirect won't work. (When I write a script for my web server, I make it nph if I do anything other than output standard pages, so I'm not sure.) You'll need to tell your web server that it's a nph script. [For Apache, you just need to call it something that begins with 'nph-', I don't know about others.]
  2. I put the $id assignment in the if statement. That should work (I haven't -c checked this code) and it eliminates having to call CGI->param('link') twice.
  3. I assumed you were sending text to the browser, rather than HTML, for the error checking, since it didn't look like HTML. If it is, take out the type parameter.

In reply to Re: Archive::Zip with cgi empty zip file by CSJewell
in thread Archive::Zip with cgi empty zip file by wapped

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.