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

I need users to upload files via web form. I've managed to get myself into a situation where those files cannot be written to disk - instead they must be written to an oracle database.

I've managed to put the binary data in and get it back out, but I need to be able to write that binary data back out to a web page.

Example:

1) User uploads a .png file.
2) I write the binary data to the db
3) I select that data from the database
4) Now what? I need to write to the page

Remember, I have no chance of getting permissions to write these files to the disk. I'm also stuck in 5.6.1 and can't use the new features of 5.8

Anyone ever come across this particular dilemna?

Thanks!

cube

  • Comment on Writing binary data directly to webpage

Replies are listed 'Best First'.
Re: Writing binary data directly to webpage
by chromatic (Archbishop) on Sep 26, 2003 at 20:59 UTC

    Sure, that's not bad. Generate image tags that are actually calls to another program:

    <img src="fetch_image.cgi?image=puppy.png" />

    Within fetch_image.cgi, read the image from the database, set the appropriate headers, and print the image:

    use CGI; use DBI; my $q = CGI->new(); my $name = $q->param( 'image' ); my $dbh = DBI->connect( 'some', 'connection', 'options' ); my $sth = $dbh->prepare(<<END_SQL); SELECT content_type, image FROM images WHERE image_name = ? END_SQL $sth->execute( $name ); my ($type, $image) = $sth->fetchrow_array(); print $q->header( $type ), $image;

    Database connecting and error checking are left as an exercise for the reader.

      Hi chromatic, You're solution worked flawlessly!! Gracias!
Re: Writing binary data directly to webpage
by dmitri (Priest) on Sep 26, 2003 at 21:11 UTC
    In addition to chromatic's comment,

    you might want to store MIME types along with the binary objects.

      In addition to dmitri's comment, You can get the mimetype during upload via:
      my $q = CGI->new; my $filename = $q->param('file_field_name'); my $metadata = $q->fileInfo($filename); my $mimetype = $metadata->{'Content-Type'};
      As documented in the CGI.pm module --
      Clayton