?
Solved

How to load compressed file from Oracle BLOB with PHP

Posted on 2011-02-15
12
Medium Priority
?
1,300 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:Etdashou
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
12 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 34901880
What compression algorithm was used before the data was put into the data base?
0
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 1500 total points
ID: 34906808
If they are compressed then, as posted above, you need to know how to uncompress them.  Oracle doesn't compress BLOBs on insert.

My guess is the code doing the extract is interfering/corrupting the data.  It's also possible that the code that inserted them is corrupting them.

Try using PL/SQL to write out a few of them and check them for validity:
http://www.dba-oracle.com/t_writing_blob_clob_os_file.htm
0
 
LVL 1

Author Comment

by:Etdashou
ID: 34906843
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Etdashou
ID: 34907712
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
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 34907737
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
0
 
LVL 1

Author Comment

by:Etdashou
ID: 34907789
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

0
 
LVL 1

Author Comment

by:Etdashou
ID: 34907803
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
0
 
LVL 1

Author Comment

by:Etdashou
ID: 34907856
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.
0
 
LVL 1

Author Comment

by:Etdashou
ID: 34920196
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.
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 34920591
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.
0
 
LVL 1

Author Closing Comment

by:Etdashou
ID: 34945468
This help me but did not totally solve my problem. I have a new questions opened now.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to recover a database from a user managed backup
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question