Results 1 to 3 of 3

Thread: simple file download script won't work

  1. #1
    Registered
    Join Date
    Nov 2011
    Location
    texas
    Posts
    2
    Rep Power
    0

    simple file download script won't work

    Hi. I'm still a noob to Perl programming. Here is a program that I got from one of the forums here (the uploader says it works). I would greatly appreciate any help at all with this!

    I get a file with zero bytes when I try like this:

    use CGI;
    $q=new CGI;
    my $path_to_files;
    my $ID;
    my @fileholder;
    $path_to_files = "/testdownloadfolder1a/testdownloadfolder1b/";
    $ID=$q->param('ID');
    print "Content-Type: application/x-download\n";
    print "Content-Disposition:attachment;filename=$ID\n\n";#try putting the full path to the files here
    open (DLFILE, "<$path_to_files/$ID") || Error();
    @fileholder = <DLFILE>;
    close (DLFILE);
    print @fileholder;


    When I try like this I get "internal server error" (it won't let me print the headers after I open the file, which is probably where the headers need to be):

    use CGI;
    $q=new CGI;
    my $path_to_files;
    my $ID;
    my @fileholder;
    $path_to_files = "/testdownloadfolder1a/testdownloadfolder1b/";
    $ID=$q->param('ID');
    open (DLFILE, "<$path_to_files/$ID") || Error();
    @fileholder = <DLFILE>;
    close (DLFILE);
    print "Content-Type: application/x-download\n";
    print "Content-Disposition:attachment;filename=$ID\n\n";#try putting the full path to the files here
    print @fileholder;

  2. #2
    Rick
    Join Date
    May 2002
    Location
    Minneapolis, MN
    Posts
    1,753
    Rep Power
    19
    My Perl was never top-notch and it's gotten pretty rusty in the past few years, but the only thing I see you haven't done that I needed for a somewhat similar script was to set the input file handle and the output handle in BINMODE. Something like:
    Code:
    use CGI;
    $q=new CGI;
    my $path_to_files;
    my $ID;
    my @fileholder;
    $path_to_files = "/testdownloadfolder1a/testdownloadfolder1b/";
    $ID=$q->param('ID');
    print "Content-Type: application/x-download\n";
    print "Content-Disposition:attachment;filename=$ID\n\n";#try putting the full path to the files here
    open (DLFILE, "<$path_to_files/$ID") || Error();
    binmode(DLFILE);
    @fileholder = <DLFILE>;
    close (DLFILE);
    binmode(STDOUT);
    print @fileholder;
    Give that a shot. Good luck!
    Rick Trethewey

  3. #3
    Registered
    Join Date
    Nov 2011
    Location
    texas
    Posts
    2
    Rep Power
    0
    Thank You! Thank You! Thank You! It works! I was just about to give up. That single statement was all I needed. You really made my day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •