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

I'm having trouble getting files to print to the screen. You can see what I've attempted to do below. What this is supposed to do is open any type of file and push it through the browser. I do know not all mime types can do this but the database only has images and pdf files but it'd be great if this would work on all possible files that the browser can load.

One of my main questions is, which MIME type should be used? I read a few lists of them and none scream "BINARY" at me. Some of the APPLICATIONs look okay but there's dozens of MIME types to try and fail with, so which do you think I should be using?

And if something is wrong with my code (or if there's a better way to push the files to the user), I'd love to hear about it.

# $location is the absolute path to the known file on the server if ($download) { my $data = qq(SELECT id, fileloc, name, description FROM files WHE +RE id = "$download"); $sth = $dbh->prepare($data); $sth->execute() or $dbh->errstr; my ($id, $location, $name, $desc); $sth->bind_columns(\$id,\$location,\$name,\$desc); if ($sth->rows < 1) { print "<center><b>ERROR!</b> Your download key was not found."; exit; } else { # print special header here binmode( STDOUT ); open (FILE, "<$location") or die "Error opening file: $!"; my ($bytesread, $buffer); while ( $bytesread = read( $location, $buffer, 1024 ) ) { print $buffer; } close (FILE); } }
Thanks Monks!


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: opening a file for printing
by gaal (Parson) on Feb 16, 2005 at 17:42 UTC
    You mean the database doesn't know the type of the file it accepted at input time? Ouch.

    You need a heuristic, which as Dominus puts it "is a fancy way of saying that it doesn't work".

    File::MimeInfo::Magic might do what you want.

Re: opening a file for printing
by friedo (Prior) on Feb 16, 2005 at 17:46 UTC
    You're opening $location using the append mode (>>) but you're trying to read from it. You're also not printing any HTTP header at all. And if you don't supply a MIME type, the browser won't know what to do with the data. What kind of data is this, and what do you want the browser to do with it?
      I know I'm not printing an HTTP header yet, that's one of the questions I had was which one I should use?

      Or should I use a module like gaal suggested that determines the mime type of the file before opening it?



      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
        You can try to guess the MIME type using File::MimeInfo, but be warned that it's not perfect and may make mistakes.

        use strict; use File::MimeInfo; # ...get $location from DB.... my $mtype = mimetype($location); open my $fh, $location or die "Could not open $location: $!"; print "Content-type: $mtype\n\n"; print while <$fh>;