By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,080 Members | 1,855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,080 IT Pros & Developers. It's quick & easy.

How do you detect image MIME type of an image in a directory?

P: n/a
How do you detect an image MIME type if you know of the image in a
directory? For example, I know of:

[PHP]
if (is_file("$myImagePath/$myImageName")) { // FIND MIME TYPE BUT HOW
I DUNNO
}
[/PHP]

The only way I could think of was to determine the extension of the
image and search through /etc/mime.types, which is horrifically
impractical but the only thing I can think of. What would you suggest
I do?

Thanx
Phil
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Phil Powell a écrit le 22/03/2004 :
How do you detect an image MIME type if you know of the image in a
directory? For example, I know of:

[PHP]
if (is_file("$myImagePath/$myImageName")) { // FIND MIME TYPE BUT HOW
I DUNNO
}
[/PHP]

The only way I could think of was to determine the extension of the
image and search through /etc/mime.types, which is horrifically
impractical but the only thing I can think of. What would you suggest
I do?

Thanx
Phil


Read manual : http://www.php.net/mime-content-type

mime_content_type
(PHP 4 >= 4.3.0)

mime_content_type()
Jul 17 '05 #2

P: n/a
Use getimagesize(). It's much more efficient than going through the "magic"
list when you know you have an image on your hand. The function returns an
array. The 'mime' element gives you the mime-type.

See http://www.php.net/manual/en/function.getimagesize.php

Uzytkownik "Phil Powell" <so*****@erols.com> napisal w wiadomosci
news:1c**************************@posting.google.c om...
How do you detect an image MIME type if you know of the image in a
directory? For example, I know of:

[PHP]
if (is_file("$myImagePath/$myImageName")) { // FIND MIME TYPE BUT HOW
I DUNNO
}
[/PHP]

The only way I could think of was to determine the extension of the
image and search through /etc/mime.types, which is horrifically
impractical but the only thing I can think of. What would you suggest
I do?

Thanx
Phil

Jul 17 '05 #3

P: n/a
My apologies, I led all of you in the wrong direction on this.

Problem comes with this: I want to change the name of an image to
something else.

Let's say I have /images/my_image.jpg

And I want to change it to: /images/my_image.xls

Obviously that would work in PHP but when your browser tries to access
it it would think it's an Excel spreadsheet and not an image and
damage ensues.... or worse, change to: /images/my_image.pif -> YIKES!

Anyway, what I have to do is this: when I decide to change image
metadata such as the image name, I have to be sure that the name that
I change the image name to is an actual "image"-like name, in other
words, if I change it to "my_image.ext" then "my_image.ext" *must* be
of a valid MIME type for an image before it is allowed to change the
name of the image to that name.

And I'm sorry but getimagesize(), in this case, can't help me at all.

Phil
"Chung Leong" <ch***********@hotmail.com> wrote in message news:<V7********************@comcast.com>...
Use getimagesize(). It's much more efficient than going through the "magic"
list when you know you have an image on your hand. The function returns an
array. The 'mime' element gives you the mime-type.

See http://www.php.net/manual/en/function.getimagesize.php

Uzytkownik "Phil Powell" <so*****@erols.com> napisal w wiadomosci
news:1c**************************@posting.google.c om...
How do you detect an image MIME type if you know of the image in a
directory? For example, I know of:

[PHP]
if (is_file("$myImagePath/$myImageName")) { // FIND MIME TYPE BUT HOW
I DUNNO
}
[/PHP]

The only way I could think of was to determine the extension of the
image and search through /etc/mime.types, which is horrifically
impractical but the only thing I can think of. What would you suggest
I do?

Thanx
Phil

Jul 17 '05 #4

P: n/a
*** Phil Powell wrote/escribió (23 Mar 2004 07:33:06 -0800):
Let's say I have /images/my_image.jpg

And I want to change it to: /images/my_image.xls

Obviously that would work in PHP but when your browser tries to access
it it would think it's an Excel spreadsheet and not an image and
damage ensues....


Browser will think it is whatever web server tells it is. When sending the
file server adds the appropriate Content-Type header. By default, web
server normally checks file extension and finds a MIME value in a chart. If
you want to change this you can either disconfigure your web server (not
recommended) or write a script to serve contents:

header('Content-Type: image/jpeg')
readfile('/home/site/htdocs/images/my_image.pif')


--
--
-- Álvaro G. Vicario - Burgos, Spain
--
Jul 17 '05 #5

P: n/a
Alvaro G Vicario <al******************@telecomputeronline.com> wrote in message news:<1f*****************************@40tude.net>. ..
*** Phil Powell wrote/escribió (23 Mar 2004 07:33:06 -0800):
Let's say I have /images/my_image.jpg

And I want to change it to: /images/my_image.xls

Obviously that would work in PHP but when your browser tries to access
it it would think it's an Excel spreadsheet and not an image and
damage ensues....
Browser will think it is whatever web server tells it is. When sending the
file server adds the appropriate Content-Type header. By default, web
server normally checks file extension and finds a MIME value in a chart. If
you want to change this you can either disconfigure your web server (not
recommended) or write a script to serve contents:

header('Content-Type: image/jpeg')
readfile('/home/site/htdocs/images/my_image.pif')


Ouch! I can't do that, the code will have redirects as well, a
header() tag would throw nasty warnings indicating that I am
overwriting the header information.

Phil

--

Jul 17 '05 #6

P: n/a
Uzytkownik "Phil Powell" <so*****@erols.com> napisal w wiadomosci
news:1c**************************@posting.google.c om...
My apologies, I led all of you in the wrong direction on this.

Problem comes with this: I want to change the name of an image to
something else.

Let's say I have /images/my_image.jpg

And I want to change it to: /images/my_image.xls

Obviously that would work in PHP but when your browser tries to access
it it would think it's an Excel spreadsheet and not an image and
damage ensues.... or worse, change to: /images/my_image.pif -> YIKES!

Anyway, what I have to do is this: when I decide to change image
metadata such as the image name, I have to be sure that the name that
I change the image name to is an actual "image"-like name, in other
words, if I change it to "my_image.ext" then "my_image.ext" *must* be
of a valid MIME type for an image before it is allowed to change the
name of the image to that name.

And I'm sorry but getimagesize(), in this case, can't help me at all.


Nothing can help those who don't wish to be helped. Look at the
documentation of getimagesize(). If you don't see a solution to your
problem, then you should quit programming altogether.
Jul 17 '05 #7

P: n/a
I found it, mime_content_type(), provided your version and instance of
PHP has magic.mime installed and instantiated within php.ini upon PHP
installation.

Phil

"Chung Leong" <ch***********@hotmail.com> wrote in message news:<oI********************@comcast.com>...
Uzytkownik "Phil Powell" <so*****@erols.com> napisal w wiadomosci
news:1c**************************@posting.google.c om...
My apologies, I led all of you in the wrong direction on this.

Problem comes with this: I want to change the name of an image to
something else.

Let's say I have /images/my_image.jpg

And I want to change it to: /images/my_image.xls

Obviously that would work in PHP but when your browser tries to access
it it would think it's an Excel spreadsheet and not an image and
damage ensues.... or worse, change to: /images/my_image.pif -> YIKES!

Anyway, what I have to do is this: when I decide to change image
metadata such as the image name, I have to be sure that the name that
I change the image name to is an actual "image"-like name, in other
words, if I change it to "my_image.ext" then "my_image.ext" *must* be
of a valid MIME type for an image before it is allowed to change the
name of the image to that name.

And I'm sorry but getimagesize(), in this case, can't help me at all.


Nothing can help those who don't wish to be helped. Look at the
documentation of getimagesize(). If you don't see a solution to your
problem, then you should quit programming altogether.

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.