Question

I have a script which is supposed to tell the file type (jpg,bmp,png).

The script is as follows:

$file="pic/shimla.png";

$finfo = finfo_open();

$fileinfo = finfo_file($finfo, $file, FILEINFO_MIME);

finfo_close($finfo);

Now say if I had a file earlier named a.jpg, which later I changed to a.png, the above script will state that the image is actually jpg, even though it is renamed to a.png

Now I want to do the same with CodeIgniter, and I wonder if CodeIgniter has some own function which can make it equivalent to the above script.

I tried to use CodeIgniter's function get_mime_by_extension('file'), but it has a problem.

If I change the a.jpg to a.png, then the above function returns image/png which is not true, it should be a.jpg, just the top-most script does.

So get_mime_by_extension('file') just returns the extension of the image, it doesn't evaluate the original nature of the image. if it is renamed to bmp, it will throw bmp, if its renamed to gif, it will throw gif.

Was it helpful?

Solution

Use getimagesize()

$result = getimagesize($file);
$mime = $result["mime"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top