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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    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("$myIm agePath/$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
  • Jedi121

    #2
    Re: How do you detect image MIME type of an image in a directory?

    Phil Powell a écrit le 22/03/2004 :[color=blue]
    > 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("$myIm agePath/$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[/color]

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

    mime_content_ty pe
    (PHP 4 >= 4.3.0)

    mime_content_ty pe()


    Comment

    • Chung Leong

      #3
      Re: How do you detect image MIME type of an image in a directory?

      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" <soazine@erols. com> napisal w wiadomosci
      news:1cdca2a7.0 403221426.776e1 889@posting.goo gle.com...[color=blue]
      > 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("$myIm agePath/$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[/color]


      Comment

      • Phil Powell

        #4
        Re: How do you detect image MIME type of an image in a directory?

        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.e xt" then "my_image.e xt" *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" <chernyshevsky@ hotmail.com> wrote in message news:<V7OdncMua 6jyMcLdRVn-jw@comcast.com> ...[color=blue]
        > 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" <soazine@erols. com> napisal w wiadomosci
        > news:1cdca2a7.0 403221426.776e1 889@posting.goo gle.com...[color=green]
        > > 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("$myIm agePath/$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[/color][/color]

        Comment

        • Alvaro G Vicario

          #5
          Re: How do you detect image MIME type of an image in a directory?

          *** Phil Powell wrote/escribió (23 Mar 2004 07:33:06 -0800):[color=blue]
          > 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....[/color]

          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
          --

          Comment

          • Phil Powell

            #6
            Re: How do you detect image MIME type of an image in a directory?

            Alvaro G Vicario <alvaro_QUITAR_ REMOVE@telecomp uteronline.com> wrote in message news:<1flb87bq6 ahkn$.2hy5wwc9e lfq.dlg@40tude. net>...[color=blue]
            > *** Phil Powell wrote/escribió (23 Mar 2004 07:33:06 -0800):[color=green]
            > > 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....[/color]
            >
            > 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')
            >
            >
            >[/color]

            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
            [color=blue]
            >
            > --[/color]

            Comment

            • Chung Leong

              #7
              Re: How do you detect image MIME type of an image in a directory?

              Uzytkownik "Phil Powell" <soazine@erols. com> napisal w wiadomosci
              news:1cdca2a7.0 403230733.445de 2d5@posting.goo gle.com...[color=blue]
              > 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.e xt" then "my_image.e xt" *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.[/color]

              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.


              Comment

              • Phil Powell

                #8
                Re: How do you detect image MIME type of an image in a directory?

                I found it, mime_content_ty pe(), provided your version and instance of
                PHP has magic.mime installed and instantiated within php.ini upon PHP
                installation.

                Phil

                "Chung Leong" <chernyshevsky@ hotmail.com> wrote in message news:<oI2dnd28I p1DYv3dRVn-gw@comcast.com> ...[color=blue]
                > Uzytkownik "Phil Powell" <soazine@erols. com> napisal w wiadomosci
                > news:1cdca2a7.0 403230733.445de 2d5@posting.goo gle.com...[color=green]
                > > 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.e xt" then "my_image.e xt" *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.[/color]
                >
                > 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.[/color]

                Comment

                Working...