Link to home
Start Free TrialLog in
Avatar of Etdashou
EtdashouFlag for Canada

asked on

How to load compressed file from Oracle BLOB with PHP

Hello everyone.

I am trying to load a blob from an Oracle database. This blob actually contains a file (can be pdf, png, doc, etc.). They are small files, < 1mb. I am able to get their filename and filetype.

However, it seems like they are compressed in the database. Is it possible to "un-compressed" them and then download those files?

Here is my simple php script that will get an attachment. When I load the page, I have the good filename + size that I can save to my computer, but the filename seems corrupted and I cannot use it. So I am pretty sure that it is compressed.

<?php
$Attachment = new Sysattachment();
$allAttachments = array();
$allAttachments = $Attachment->getAllAttachmentFromId('IM530717');

header("Content-length: {$allAttachments[1]['SIZEPRGN']}");
header("Content-type: {$allAttachments[1]['MIMETYPE']}");
header("Content-Disposition: attachment; filename={$allAttachments[2]['FILENAME']}");
$foo = "";
                   
while(!$allAttachments[2]['DATAPRGN']->eof()){
    $foo .= $allAttachments[2]['DATAPRGN']->read(2000);
}
print $foo;
?>

Open in new window


Can we un-compress a file from the database? With the Select Statement?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

What compression algorithm was used before the data was put into the data base?
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Etdashou

ASKER

hmm, I will try to discover what is the compression algorithm. I will let you know what I discover.

Also I will follow your link.
I don't know if this help, but this is the response I got from the support of the software that does the compression:

When attachments are compressed in our software the functions that perform this compression reference the zlib compression library, an open source free to invoke lossless compression library.

Therefore to uncompress the attachments a utility that references this library can be used, for example gzip from Gnu, or built to extract the full size attachment.

If I can get the content with PHP, can I un-compress it with a php function?

Thank you
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Looks like the BLOBs are compressed.  No need to use the PL/SQL to extract them as a test.


I'm not a php person but looks like there is a zlib library available:

http://php.net/manual/en/book.zlib.php
I tried the function gzuncompress but  I still don't have a good result... If anyone can help, I would greatly appreciate.

<?php
$Attachment = new Sysattachment();
$allAttachments = array();
$allAttachments = $Attachment->getAllAttachmentFromId('IM530717');

header("Content-length: {$allAttachments[1]['SIZEPRGN']}");
header("Content-type: {$allAttachments[1]['MIMETYPE']}");
header("Content-Disposition: attachment; filename={$allAttachments[1]['FILENAME']}");
$foo = "";
                   
while(!$allAttachments[1]['DATAPRGN']->eof()){
    $foo .= $allAttachments[1]['DATAPRGN']->read(2000);
}
print gzuncompress($foo);
?>

Open in new window

And I have this error message if I don't add the "CONTENT" header.


Warning: gzuncompress() [function.gzuncompress]: data error in \attachment\load.php on line 21
hmmm...

I may need to solve a problem before.

Here is my SQL Statement:
$sql = oci_parse($c, "select \"SYSATTACHMEM1\".\"TOPIC\", \"SYSATTACHMEM1\".\"APPLICATION\", \"SYSATTACHMEM1\".\"DATAPRGN\", \"SYSATTACHMEM1\".\"MIMETYPE\", \"SYSATTACHMEM1\".\"SIZEPRGN\", \"SYSATTACHMEM1\".\"FILENAME\", \"SYSATTACHMEM1\".\"DATE_ADDED\" from SYSATTACHMEM1 where \"SYSATTACHMEM1\".\"TOPIC\"='$id'");
		
$resultat = oci_execute($sql);

Open in new window


If I run this query, it works, but I have a error message displayed:
Warning: Unknown: OCI_INVALID_HANDLE in Unknown on line 0

If I remove this part of the sql statement: \"SYSATTACHMEM1\".\"DATAPRGN\"

And run the sql again, i don't have the error message. DATAPRGN is the actual file content, so perhaps their is something I need to do for this?

Tahnk you.
Ok,

Forget about the OCI_INVALID_HANDLE. I was assigning an object from the SQL call...

That said, I now have an array like that:

Array ( [TOPIC] => IM530717 [MIMETYPE] => image/gif [SIZEPRGN] => 1285 [FILENAME] => dashboard.gif [DATAPRGN] => "a lot of stuff that I can't read... Example: _RCFM*=.BxÚs÷t³°LT`P`øοÆÆÆVVVNNN.n®ÎÞ¾žžž...etc")

I need to be able to download the DATAPRGN now, but I always have a corrupted file.
The company say it is zlib compression. So I try this:

header("Content-length: {$allAttachments[1]['SIZEPRGN']}");
header("Content-type: {$allAttachments[1]['MIMETYPE']}");
header("Content-Disposition: attachment; filename={$allAttachments[1]['FILENAME']}");
echo gzuncompress($allAttachments[1]['DATAPRGN']);

But it doesn't work.

I hope you guys understand and if I need to be more clear about all that, please tell me.
Sorry.  I'm out since this now appears to be strictly PHP related.

I suggest you use the 'Request Attention' link above to see if a Moderator can stir up some new Experts for you.
This help me but did not totally solve my problem. I have a new questions opened now.