detect binary file's mime type

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
matt86
Forum Newbie
Posts: 2
Joined: Sat Aug 20, 2011 12:28 pm

detect binary file's mime type

Post by matt86 »

Hi guys,
I have a question, how can detect a binary file's mime type?

I am creating a system API that allows the client to upload files within my server via API calls. The client must send the image into binary like this:

Code: Select all

function ImgToBinary($pathImg)
{
    $con = file_get_contents($pathImg);
    $en = base64_encode($con);
    return $en;
}  
Then comes an image to the server in binary base64-encoded. My server will decode it and save it in a folder.
To decode it, I wrote:

Code: Select all

$image = base64_decode($my_binary_image_base64);  
for save the image, i want use "file_put_contents" but first i must detect the mime type of decoded image. How can I do?

Thanks

matt86
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: detect binary file's mime type

Post by Christopher »

(#10850)
matt86
Forum Newbie
Posts: 2
Joined: Sat Aug 20, 2011 12:28 pm

Re: detect binary file's mime type

Post by matt86 »

Sorry Christopher,
But i must know first the mime type and after save the image with file_put_contents.
The finfo function want the path of image.

I must detect the binary file's mime type first.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: detect binary file's mime type

Post by Christopher »

The only thing you need to be sorry about is that you did not look around in manual a little more. If you taken a few seconds to look at the other methods in finfo, you would have found this:

http://www.php.net/manual/en/function.finfo-buffer.php
(#10850)
Post Reply