Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How do I enter and retrieve a GIF File from mysql via CGI?

by Anonymous Monk
on Jul 04, 2000 at 17:36 UTC ( [id://21009]=perlquestion: print w/replies, xml ) Need Help??

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

How do I enter and retrieve a GIF File from mysql via CGI?
  • Comment on How do I enter and retrieve a GIF File from mysql via CGI?

Replies are listed 'Best First'.
Re: How do I enter and retrieve a GIF File from mysql via CGI?
by athomason (Curate) on Jul 10, 2000 at 05:46 UTC
    MySQL represents binary data via the BLOB datatype. There are actually four types of blob: TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB, which respectively can hold up to 2^8, 2^16, 2^24, and 2^32 bytes of binary data. You can treat blobs just like any other text variable, since Perl correctly handles binary data in scalars anyhow.

    To get a file from a browser, you'll need to use a file upload field. Take a look at the advise other Monks have given on this topic here; using CGI.pm is both the easiest and most foolproof way to go. CGI.pm will put the contents of the file into a scalar for you; to get it into the database, construct an INSERT with placeholders and pass the do function your file variable.

    When you want to return the file to the user, you first need to retrieve it from the database with an appropriate fetch command. Once you have it in a scalar, you should print the appropriate header for your filetype (here, "image/gif") and then print your variable. CGI.pm can once again help you out here; this statement will do what you need:

    print header(-type='image/gif'), $data;

Re: How do I enter and retrieve a GIF File from mysql via CGI?
by Anonymous Monk on Jul 04, 2000 at 20:20 UTC
    You need to have a table witha BLOB column (you choose the size of BLOB depending on the maximum size of data you want to stuff in there.

    Then, slurp your gif into a variable, and use the statement like

    $sql = "INSERT INTO BLOBTABLE (Filename, Data) VALUES (?, ?)"; my $sth = $dbh->prepare($sql); my $result = $sth->execute($filename, $gifdata);
    And to get it back, something similar... see this for an example.

    Now, to upload the image via CGI, you need to look at file uploads. To download it, you just need to print it to STDOUT with the correct content type header.

      and that content-type header is:

      Content-type: image/gif

      perl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://21009]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-24 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found