Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Tomasz Chmielewski
March 11, 2010, 4:06 pm
rate this thread
I have a database which has a lot of files saved as blobs (some "fancy" CMS
I would like to save them as files.
I saved them as files using such a query (for each blob):
my $sql = $db->prepare("SELECT blob_data FROM tx_drblob_content WHERE
uid = (?)");
$sql->execute($uid); # $uid is ID of the blob in the database
my $blob = $sql->fetchrow_array;
open BLOBFILE, ">$datadir/$uid" or die "Cannot open $!";
print BLOBFILE $blob;
Unfortunately, the files (PDF, ZIP etc.) are corrupted.
I "uploaded" a text file to the database using system's web interface, then
fetched it with the above perl code.
Here are some example differences (- denotes original file; + denotes the file
fetched with perl):
-# From ``Assigned Numbers'':
+# From ``Assigned Numbers'':
So we can see that the file has \ appended in front of each '.
Which could be because the CMS system stores the files as such, or perhaps I
should fetch/save the files differently?
Does anyone have some obvious thoughts on why I see \ appended before certain
Re: trying to extract blob as file... is corrupted
Use 3-arg open. Use lexical filehandles. Since you're printing binary
data, you should use 'binmode' or the :raw layer.
open my $BLOBFILE, ">:raw", "$datadir/$uid" or die "...";
If this is important work you should check the return value of close.
(It's not useful to check the return value of print: checking close is
both necessary and sufficient.)
Which database are you using? Which DBD? What type is the 'blob_data'
Can you get the field out using the DB's own command-line tool (psql, or
equivalent for other databases) to compare?
Well, it looks to me as though the data has been SQL-quoted, since ' is
a special character in SQL but ` isn't (depending on the dialect, of
course). However, without knowing where the quoting is happening (in the
database, in the client library, in the DBD) it's hard to say.
- » FAQ 8.12 How do I start a process in the background?
- — Next thread in » PERL Discussions