Since you have the image's contents in a string, you can use getimagesizefromstring() which contains a mime field that will tell you the type of the image.
$data = base64_decode($data);
$info = getimagesizefromstring($data);
$mime = $info['mime'];
If you need support for any file, not just images, you'll need to either write a custom file parser or, if you have the FileInfo PECL extension, you can use finfo_file():
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, '/path/to/your/file.jpg');
The caveat to this is that you need an actual file, not a string (but you could always write the string to a temporary file).
Here's the answer to solve the question without the need of a file being created. Let's take the following data from an image:
data:image/jpeg;base64/9j/4AAQSkZJRgA [...] xD5epeRHJ4glkjk/sb7R5lrFJ5fmfu/wDv5W1TEV/5wp+58
You would need to first parse it first
$data = explode(",", $data);
Then insert the string in the buffer
Finally, use the answer from @JohnErck to solve the extension type.
file_put_contents($path . $filename . AppUtil::FileExt($data), $data);
So I would save the file with no extension, then read it from finfo and would deduct the extension from that?
@MichaelVilleneuve Yeah; file-types aren't solely dependent on the file's extension (for instance, you can save a jpeg as img.png and it will still open/render fine). Almost all files have headers in them that declare what their actual file-type is.
I'm on it, will get back to you with result, thanks for your time (no idea why someone downvoted you!)
You mind if I edit your answer, it's working but I would make a few modification for future user that would experience this issue?
@MichaelVilleneuve Feel free; make any changes to it that were necessary to solve your problem that could also help out others =]