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
Solved

Detecting Content-Type of BLOB

Posted on 2014-10-21
9
99 Views
Last Modified: 2014-10-21
I am opening an Access file using PHP. One of the fields stores a BLOB. According to the notes attached to the DB the the stored image can be in one of two forms , to quote: "JPEG (lossy)" or "LZH (lossless)". Is there a way of detecting the type using PHP in this situation?
0
Comment
Question by:cescentman
  • 4
  • 3
  • 2
9 Comments
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 40394186
Each of those formats has a header record. You can check the first 4-10 bytes to see which header type they match

Here is some info on the header types

JPG
http://www.onicos.com/staff/iz/formats/jpeg.html
http://www.fastgraph.com/help/jpeg_header_format.html

LZH
http://www.onicos.com/staff/iz/formats/lzh.html
0
 
LVL 1

Author Comment

by:cescentman
ID: 40394190
Thanks do I just read the first 10 bytes of the blob and examine last 6 then?
0
 
LVL 1

Author Comment

by:cescentman
ID: 40394210
I've tried:-

substr(base64_encode($arrImage['thumbnail']),1,80)
substr($arrImage['thumbnail'],1,80);

Neither produces anything recognisable.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 55

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40394235
What about
// UNPACK THE FIRST 10 BYTES AS 
// unsigned char
$array = unpack("C10", $arrImage['thumbnail']);

// CHECK FIRST 3 BYTES AGAINST JPG HEADER FF D8 FF
if ($array[1] == 0xff && $array[2] = 0xd8 && $array[3] = 0xff) 
	echo "This is a JPG file";
else
	echo "This is NOT a JPG file";

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40394239
Please post the test data.  We do not need to see code that doesn't work - just the data you're trying to examine.

Also, I would just add that a BLOB in a database is almost always a code smell and the signature of a misdesigned application.  We don't do that any more (the issues go to the performance of the database and the ability to back it up), instead we store the URL of the "blob-type" data in the database and store the data in the file system.  You might consider a design change.
0
 
LVL 1

Author Comment

by:cescentman
ID: 40394270
Ray

I've read your posting and I am grateful for the time you take to post it. However in this case I posted the code as evidence that I am indeed trying myself not just relying on julianH. I think this was a reasonable thing for me to do. In return julianH has provided me with a solution that does work magnificently and in the course of it I have increased my knowledge.

As to your second point I'm unflattered that you feel the need to tell me something I already know.  You also assume I have control over the database design. Don't most of us find ourselves in the situation where we have no choice but to bolt onto other people's developments?
0
 
LVL 1

Author Closing Comment

by:cescentman
ID: 40394282
Great help thanks.
0
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 40394355
You are welcome - thanks for the points and good luck with the project.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40394379
Hi cscentman.  Sorry I don't know what you know or don't know; it's just not something that was included in the dialog here at E-E, so we have to make some assumptions.  There are plenty of people who have control over a database design and would be glad to learn that a BLOB was a bad idea and that the file system was the right place for blob-like data.  As to asking for the test data, well, with test data we can give you tested examples.  Without test data we cannot.  We can guess at what you know and sometimes that's enough, but there really is no substitute for the SSCCE.  This article explains why it's worth having test data and shows how we use it to get better, faster results.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

Good luck with it, ~Ray
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP not parsing ' character 12 46
Could anyone can give me a suggestion about good client friendly theme. 7 29
XML extra information 8 29
Row insertion failed. Array 5 46
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

840 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