How do I get a file extension that is not on it?
I have some pictures in our catalog c:/images/
. For example:
- 1.jpg
- 1.png
- 2.gif
- 3.jpg
And I only have the image name without the extension . I know c:/images/
there are images with names like:, 1, 2, 3, ...
and I don't know their extensions.
How do I get a file extension that doesn't have it?
I tried using:
var_dump(pathinfo ('c:/images/1'));
But this only returns:
array(3) { ["dirname"]=> string(3) "c:\images\" ["basename"]=> string(1) "1" ["filename"]=> string(1) "1" }
source to share
getimagesize () returns (among other information) the MIME type of the image. So use
var_dump(getimagesize('c:/images/1'));
and you will get the MIME type in the array element mime
. For example. image/jpeg
or image/png
.
There's also a faster foundation exif_imagetype () , but this function only returns a MIME integer. The table corresponding to the possible return values ββfor the image type can be found in the PHP manual.
source to share
If you have, finfo_file()
can return the file type mime:
$finfo = finfo_open(FILEINFO_MIME_TYPE);
var_dump(finfo_file($finfo, '/var/www/favicon.ico'));
This will print:
line (12) "image / x-icon"
Extensions don't mean much on the internet. Browsers and (web servers) don't really use them. They look at the type of mime. The browser will detect a JPEG named image.png
as JPEG.
source to share