Page 1 of 1

detect binary file's mime type

Posted: Sat Aug 20, 2011 12:39 pm
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

Re: detect binary file's mime type

Posted: Sat Aug 20, 2011 12:58 pm
by Christopher

Re: detect binary file's mime type

Posted: Sat Aug 20, 2011 1:17 pm
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.

Re: detect binary file's mime type

Posted: Sat Aug 20, 2011 6:48 pm
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