Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This is to Anonymous Monk, I couldn't pm you a questionsion since you didn't sign in. This is what happened while running your code. I tried using this and it kept erroring out. I ended up having to use CGI qw/:standard/ instead of Simple, but those little changes shouldn't be causing the problem.

I uploaded foobar.txt in the same folder using an absolute path and at run time I get PK=8†0 foobar.txtadadasddsC™ PK=8†0C™ ¶foobar.txtPK8=.

This proves two things though. It proves it can find the foobar.txt and is goofing around with it and also proves it can read it "adadasdds" is the entirety of the text file. I get this text message to browser, it doesn't die or anything and no popup window exists. What should I do?

#!/usr/bin/perl use warnings; use CGI::Carp 'fatalsToBrowser'; $|++; use strict; use CGI qw/:standard/; use Archive::Zip; my $fileToUpload = 'foobar.txt'; my $zip = Archive::Zip->new(); open( my $fh, '<', $fileToUpload ) or die "open failed: $!"; binmode( $fh ); # for win32 my $contents = do { local $/; <$fh> }; close( $fh ); $zip->addString($contents, $fileToUpload); print header( -type => 'application/zip', -attachment => "$fileToUpload.zip" ); binmode( STDOUT ); # once again, for win32 $zip->writeToFileHandle(\*STDOUT);

Replies are listed 'Best First'.
Re: question about a reply to the Zip::Archive post
by tachyon (Chancellor) on Apr 08, 2004 at 03:47 UTC

    What you are seeing in the browser IS the zip file. The PK blah bit is the standard zip header - have a look at a zip file with a text editor.

    Anyway the reason you see the file as a text file is because you script is not sending a header your browser understands or your browser does not know how to deal with application/zip MIME type and defaults to text. Depending on how you look at it.

    Change the header type sent by your script to 'application/octet-stream' and it should work fine. If not get a decent browser. What are you using?

    cheers

    tachyon

      You were totally right, tachyon, thank you so much for your help! And to answer your question, I am using IE 6.0.2800... . I tried this out and it works PERFECTLY, so thanks so much for your help!

      ++ for you!

      I ran into another problem that a few hours trying to debug wouldn't help. M problem is the files I'm including are not in the same dirctory as the script. A my $fileToUpload = 'foobar.txt'; runs perfectly and as expected if the file is inside the same folder. I tried my $fileToUpload = "/home/spanky/public_html/scripts/upload/images/VwImg.jpg"; and it doesn't work. It doesn't work because it has the / in the file name $fileToUpload. So I tried changing the attachment to -attachment => "test.zip" and it inserts an empty folder.

      Do you have any advice how I would include files from other directories?

      Thank you for all your help.

        my @path = split '/', $fileToUpload; my filename = $path[-1]; $zip->addString($contents, $filename); print header( -type => 'application/zip', -attachment => "$filename.zip" );

        cheers

        tachyon

Re: question about a reply to the Zip::Archive post
by Mr. Muskrat (Canon) on Apr 08, 2004 at 02:10 UTC

    What should I do?

    Reply to the node in question!

      I have but a few days have passed and heard not a reply. Who's to say all of them will look back at nodes they made since they don't get notified of responses to it?
Re: question about a reply to the Zip::Archive post
by Anonymous Monk on Apr 08, 2004 at 05:00 UTC

    Ah sorry, that was my reply you were questioning. Since your problem was the content type header being provided, all I'll say in my defense is that it is your browser that is at fault. application/zip should be recognized as a zip file and handled accordingly. application/octet-stream is more for .exe files, but will also work for this situation. Just to be curious, what browser did you use that printed out the zip file contents in the client window? I tried it in both Opera and Internet Explorer and it worked fine for me :)