It could be that the output file is not in binary format. Try using the command :
binmode (OUTPUT) just after opening the file. -----Original Message----- From: Karl Kopper [mailto:[EMAIL PROTECTED] Sent: Wednesday, March 03, 2004 1:25 PM To: [EMAIL PROTECTED] Subject: Stuck on a BLOB Hi All, I've googled for hours and banged my head, but no luck. What am I doing wrong? I'm trying to pull a PDF blob out of MySQL on Linux: Step 1 - Check the PDF file before putting it in the database. /*Before Size*/ # ls -l /tmp/orig.pdf -rw------- 1 daemon daemon 14288 Mar 3 09:38 /tmp/orig.pdf /*Before Checksum*/ #md5sum /tmp/orig.pdf 614aff27a25182fa4c5d6b6740e34836 /tmp/orig.pdf /* Sanity checks*/ #head -1 /tmp/orig.pdf %PDF-1.2 #pdf2ps /tmp/orig.pdf /tmp/orig.ps /* No errors are returned so the PDF file looks okay.*/ Step 2 - Put the PDF into MySQL as a BLOB /usr/bin/mysql -h SQLSERVERHOST -u dbuser -p dbpass database UPDATE mytable set myblobcolumn=LOAD_FILE("/tmp/orig.pdf") where mykey="1"; /* I know the PDF is okay because I can now read it and display it with a Python CGI using a web browser. */ Step 3 - Read it with PERL DBI #!/usr/bin/perl -w use DBI; use strict; my $user = "user"; my $password = "password"; my $dsn = "dbi:mysql:database"; my $dbh = DBI->connect($dsn, $user, $password) || die "Cannot open db"; my $sql = "SELECT * from mytable where mykey = '1'"; my $sth = $dbh->prepare($sql, { 'LongReadLen' => 1638400 }); my $numrows = $sth->execute; open OUTPUT, ">/tmp/extracted.pdf"; my $ref = $sth->fetchrow_hashref; my $newdata = $$ref{'myblobcolumn'}; print OUTPUT $newdata; close OUTPUT; $sth->finish; $dbh->disconnect; /* I used a very long and very arbitrary read length. This program runs without errors. */ Step 4 - Try to use the PDF file I just extracted. #ls -l /tmp/extracted.pdf -rw-r--r-- 1 root root 19893 Mar 3 09:53 /tmp/extracted.pdf /* It is bigger! */ # md5sum /tmp/extracted.pdf 8c145ae11c464bce0ac88cf775828d13 /tmp/extracted.pdf /* Its checksum changed*/ # head -1 /tmp/extracted.pdf %PDF-1.2 /* Still 'might' be useable PDF?? */ # pdf2ps /tmp/extracted.pdf Error: /syntaxerror in readxref Operand stack: Execution stack: %interp_exit runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 1 3 %oparray_pop 1 3 %oparray_pop --nostringval-- --nostringval-- --nostringval-- --nostr ingval-- --nostringval-- --nostringval-- Dictionary stack: --dict:1051/1123(ro)(G)-- --dict:0/20(G)-- --dict:92/200(L)-- --dic t:92/200(L)-- --dict:97/127(ro)(G)-- --dict:229/230(ro)(G)-- --dict:14 /15(L)-- Current allocation mode is local GNU Ghostscript 7.05: Unrecoverable error, exit code 1 /* Nope. It is not valid PDF code anymore */ Where did I go wrong? --Karl
