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

Oh great gurus of the ether!

I'm trying to allow a person to upload a document, using the 'file' input variable on a web form. I want to then save the document as a blob in a database.

The database is mySQL and, of course, I'm using DBI/DBD for access.

Replies are listed 'Best First'.
(ichimunki) Re: DBI file input to blob
by ichimunki (Priest) on Aug 06, 2001 at 20:02 UTC
    Not to be persnickety, but what is your question?

    I don't normally write responses like this, but you probably should show us some code and tell us what kind of errors you are getting.

    I also suggest writing the database routines distinctly from writing the CGI/Form-handling routines. That way you can exercise and test the database routines separately. Once you have that, you make sure your CGI script is able to even receive the text/file and capture it in a variable (think "echo input back to user"). Once both pieces work independently, you link them together by replacing your CGI echo routine with a call to your database input routine using your variable as an input.
Re: DBI file input to blob
by foogod (Friar) on Aug 06, 2001 at 20:42 UTC
    amracel,

    Begin with File Upload To Selected Directory. This should answer questions on writing the CGI to parse the form and place the data into a variable.

    Then check this out MySQL: BLOB/TEXT. This should give you more understanding on MySQL blob(actually it is called TEXT).

    And finally check out MySQL & DBI for standard connection code, and other mundane stuff.

    And as a final word of caution IF the data that you are going to be putting into the TEXT/BLOB field is very large (like say 1 meg IMAGES!) your database will become HUGE, very very fast. In this case I would recomend simply writing the uploaded file to your webserver, and then placing a file pointer (non C++ reference! :-D )in a varchar filed (i.e. the link to the file such as "/home/httpd/htdocs/files/super_cool.txt") and then using the file pointer in your script.


    HTH

    -f o o g o d

    --- ruining the bell curve for everyone else ---

Re: DBI file input to blob
by amracel (Initiate) on Aug 06, 2001 at 21:14 UTC

    sigh! The code, which isn't working is:

    # if a syllabus was  attached
    
        if($query->param('syllabus')) {
    	while($bytesread = read($query->param('syllabus'),$buffer,1024)) {
    	    $syllabus .= $buffer;
    	}
    	$sylName = $query->param('syllabus');
    	$sylName =~ s/.*\/\\(.*)/$1/;
        }
        $sql = "INSERT INTO class_info(title, instructor, inst_email, " .
               "active,max_students, description,syllabus,begin_date, " .
    	   "syll_name,begin_time,class_length) VALUES('" . 
    	   $query->param('title') . "','" .
    	   $query->param('name') . "','" . $query->param('email') . "',1," .
    	   $maxstud . ",'" . $query->param('description') . "'," .
    	   $syllabus . ",'" . 
               $query->param('year0') . "-" . $query->param('mon0') . "-" .
    	   $query->param('day0') . "','" . $sylName . "'," .
    	   "SEC_TO_TIME(TIME_TO_SEC('" . $query->param('classtime') . "') + " .
    	       $timeadj . ")," . $query->param('lengthofclass') . ")";
        $dbh->do("INSERT INTO class_info(title, instructor, inst_email, " .
               "active,max_students, description,syllabus,begin_date, " .
    	   "syll_name) VALUES('" . $query->param('title') . "','" .
    	   $query->param('name') . "','" . $query->param('email') . "',1," .
    	   $maxstud . ",'" . $query->param('description') . "'," .
    	   $syllabus . ",'" . 
               $query->param('year0') . "-" . $query->param('mon0') . "-" .
    	   $query->param('day0') . "','" . $sylName . "'" .
    	   "SEC_TO_TIME(TIME_TO_SEC('" . $query->param('classtime') . "') + " .
    	   $timeadj . ")," . $query->param('lengthofclass') . ")";
    	|| die $sql . "\n" . $dbh->errstr;
    
      Hello

      You should first prepare your statement then execute it. Look at the DBI manual for how you can do that. Basically your code should look something like:

      my @hdr = qw/title instructor inst_email active max_students description syllabus begin_date syll_name begin_time class_length/; my @val = map "?", @hdr; my @real_val = map{param($_)}@hdr; my $sql; { local $" = ','; $sql = "insert into class_info(@hdr) values (@val)"; } my $sth = $dbh->prepare($sql); $sth->execute(@real_val);
      This is a much cleaner approach and you don't need to worry about escaping noise and escaping the binary attachment since you can include the variable holding the binary data (the blob) directly to the execute statement.

      Add sanity checks and dies as you want. They were removed for readability.

      Aziz,,,

      "isn't working" is a bit vague ... at best. What is the error you are getting? Without the whole code, I would have to program my own version of this and then verify yours from there.

      Post your error code, and perhaps we can go from there.

      - f o o g o d

      --- ruining the bell curve for everyone else ---

Re: DBI file input to blob
by amracel (Initiate) on Aug 06, 2001 at 22:35 UTC
    Thanks for the responses.

    Unfortunately, there is no error message. The problem is that I get no input: the data field is blank after the operation. That is, I check the database for what's written and nothing is there.

    I've used a similar technique to read the data from an input file, then write it to a flat file, so I know it should work.