Solved

How to load compressed file from Oracle BLOB with PHP

Posted on 2011-02-15
12
1,236 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
  • 7
  • 3
12 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
What compression algorithm was used before the data was put into the data base?
0
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:Etdashou
Comment Utility
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 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:Etdashou
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 76

Expert Comment

by:slightwv (䄆 Netminder)
Comment Utility
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
Comment Utility
This help me but did not totally solve my problem. I have a new questions opened now.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
The viewer will learn how to count occurrences of each item in an array.

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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now