473,385 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
7 9082
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
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
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
*** 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: GAR | last post by:
I recently set up ampache so I can have an mp3 server. I dont like how they display album covers so I decided to try and change it a little. Instead of taking the album cover from the mp3s...
3
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a...
5
by: Stella | last post by:
Hi, I am attempting to create a site which validates as XHTML 1.1. So far I have got it fully validated but I am not happy with the way Internet Explorer displays the site when the window is...
7
by: Dobedani | last post by:
Dear All, I am developing / maintaining a web application which generates GIF images on the fly. When I send the image, I make sure a header is sent first with MIME-type image/gif. My HTML-code...
1
by: Grzegorz Kaczor | last post by:
Hello, I wanted to use Response.TransmitFile to return an image instead of a HTML page. Under IE everything works well, but under Mozilla/Firefox the image is treated as it were text/html. ...
2
by: Adam Teale | last post by:
hey guys Is there a builtin/standard install method in python for retrieving or finding out an image's dimensions? A quick google found me this:...
7
by: xx75vulcan | last post by:
Hi, I've got a PHP Upload Form that works great, unless that is, the image your uploading has been modified through a photo editing software. Example: if I upload the image straight from a...
7
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Hi. I have an ASP.NET 2.0 web application which contains an Images directory with all website images. How can I prevent other websites from creating img tags with the source as my images? I want...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.