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

Hello everybody,
in short words: I want to upload a binary data with a html form, handle it with a perl script and store it in a mysql blob field. I have created an upload-form with html:
<form action="upload_process.pl" method="post" enctype="multipart/form +-data"> <input type="file" name="myfile" accept="text/*" maxlength="2097152"> <!-- hier beliebige andere Felder --> <input type="submit" value="RESSOURCE" name="action"><input type="rese +t"> </form>
And now, I want to insert the file in a mysql database (blob field). Here is the upload_process script:
... my $q = new CGI; my $file = $q->param("myfile"); binmode $file; &insertRessource( $name, $entryid, $file, $mime ); #all parameters are available
And here is the insert function:
sub insertRessource { my $name = $_[0]; my $entryid = $_[1]; my $file = $_[2]; my $mime = $_[3]; my $sth = $dbh->prepare('insert into ressource (name, entryID ,dat +a, mime) values (?,?,?,?)'); $sth->execute($name, $entryid, $file, $mime) or die $!; $sth->finish; $dbh->disconnect; }
This code is working but - sadly - not correct ;) I can see blob-data in the binary field but it contains only the filename of the uploaded document (about 7 Bytes). Can anybody help me? Thanks miggel15

Replies are listed 'Best First'.
Re: How to manage post binary upload with mysql blob fields
by Anonymous Monk on Feb 15, 2011 at 07:58 UTC
      Hello and thanks for your help. I read it but I know, how to save an uploaded file. The problem is, that I don't know how to save the uploaded file into a database blob field. Any hints?
        Try reading to $file in blocks
        my $fh = $q->param('myfile'); binmode $fh; my $block; my $file; while ( read($fh,$block,1024) ){ $file .= $block; } insertRessource( $name, $entryid, $file, $mime );
        poj
Re: How to manage post binary upload with mysql blob fields
by locked_user sundialsvc4 (Abbot) on Feb 15, 2011 at 14:36 UTC

    Super Search is your best friend, second only to Google.   http://www.perlmonks.org/?node_id=150255

    There are two important considerations here:

    1. In my experience, putting the actual data in a database is sometimes much less satisfactory than putting the data in a disk-file at a known location, and putting a reference to that data in the database.
    2. If you do move a large binary object into a database field, it is important that you do so in a way that does not require an inordinate amount of memory consumption ... at least, not within/by the web server.   You may also find that you can’t afford to take the time.   You may have to arrange for the actual transfer to be done by a separate process that exists only for this purpose – apart from both the web-server and the database-server processes.