Get file type from base64 php

If you know a minimal amount about the file format structure, you could theoretically look at the top bytes of the file until you could work out what type of file it is.

For example, a GIF image always starts with the following bytes GIF89a. If you can find that string at the begining of the file, you can be reasonably sure that it is a GIF image and absolutely certain it isn't any other image format. [it could still be a text file though, that just happens to start with 'GIF89a'; you'd have to parse more of the file to be absolutely certain]

Likewise, PNG files have the string PNG fairly near the start [it's not quite at the very begining; again, you'd need to research the file format specifics to help you determine how much you'd need to know to be certain].

JPEGs also contain recognisable strings in their headers, although these are more varied and complex. You might want to look out for the string Exif.

Getting the file format definitions would definitely give you more accuracy, but depending on how accurate you need to be, you might learn enough about the files formats just by opening some image files in a binary editor to see how they're structured.

These resources may help you:

  • //www.w3.org/Graphics/JPEG/

  • //www.w3.org/Graphics/PNG/

In this tutorial, we will let you know how to handle the image encoded with Base64 and write the image to folder. 
While we have working with API for mobile or web application , You will notice that they will send the format of images in Base64 encoded. So in that case, you will need to move the Base64 encoded image to server as a image file.and have to save it in the folder

Example Core PHP:

 

Upload base64 to file in Codeigniter 

Are you looking for a way to save a PNG image server-side, from a base64 data string, or unable to upload to a base64 encoded image using Codeigniter? We can help you with that.

We are creating some function for using base64 string check is valid or not and size and file type

create file application/libraries/Base64fileUploads.php

Chủ Đề