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

Dear monks
I am still trying to download a file, and have this script, but for some reason its giving me an error message like this : The server can't open the file: Invalid argument and this is the html for the download of the file
<html><head> <title>Download Page</title> </head> <body> <form action="/cgi-bin/download.cgi"> <p> <select size="1" name="ID"> <option value="0001.jpg">File One </option> <option value="file.zip">File Two </option> </p> </select> <input type="submit" value="Submit" name="B1"> </form> <br><br>...or here is an example of using a link to call the script: + <a href="/cgi-bin/download.cgi?ID=file.zip">Download File</a> </body> </html>
and here is the CGI for it
#!c:\perl\bin\perl.exe use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); my $files_location; my $ID; my @fileholder; $files_location = "c:\files\err"; $ID = param('ID'); if ($ID eq '') { print "Content-type: text/html\n\n"; print "You must specify a file to download."; } else { open(DLFILE, "<$files_location/$ID") || Error('open', 'file'); @fileholder = <DLFILE>; close (DLFILE) || Error ('close', 'file'); print "Content-Type:application/x-download\n"; print "Content-Disposition:attachment;filename=$ID\n\n"; print @fileholder } sub Error { print "Content-type: text/html\n\n"; print "The server can't $_[0] the $_[1]: $! \n"; exit; }
Can someone help me to find out where is the error? Thanks

Replies are listed 'Best First'.
Re: Error of the argument
by derby (Abbot) on Dec 27, 2007 at 19:31 UTC

    I wouldn't mix dos path separators with unix ones:

    $files_location = "c:/files/err";
    And I would use the three arguments version of open:
    open( DLFILE, "<", $files_location . "/" . $ID ) ...
    If that still causes problems, I would probably use File::Spec to create the pathname (all that back slashing for Win32 drives me nuts).

    -derby
      I wouldn't mix dos path separators with unix ones

      ...not only that, the specification "c:\files\err" is wrong by itself, because backslashes are special within double quotes (e.g. \f represents a form feed, and \e the escape char).

      Including the filename in the error message often is a good idea. For instance,

      my $filename = "$files_location/$ID"; open(DLFILE, "<", $filename) || Error('open', "file '$filename'");

      In that case, the OP would have gotten something like

      The server can't open the file 'c:ilesrr/...': Invalid argument 
      

      which might have hinted at the fact that there's something wrong with the filename...

Re: Error of the argument
by CountZero (Bishop) on Dec 27, 2007 at 19:46 UTC
    What is in the server error log?

    To get more info from a "die" error, you could use CGI::Carp::DebugScreen which makes nice and informative error screens (with next to no effort).

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James