SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: MIME types.

  1. #1
    SitePoint Zealot _theworks's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MIME types.

    Hi all,

    i made an upload script which checks the files extension using pathinfo and then checking the mime type using $_FILES['userfile']['type'] i then compare this infomation to two arrays of valid mime types and extensions...
    the script works somthing like this...

    PHP Code:
        $image_mime_types = array(
            
    "image/gif","image/png","image/tiff",
            
    "image/bmp","image/jpeg","application/x-shockwave-flash",
            
    "application/octet-stream","image/photoshop","image/tiff",
            
    "image/tif","image/pjpeg","image/x-png");
            
        
    $image_extensions = array("gif","png","tiff","jpeg","jpg","bmp","swf","psd"); 
    PHP Code:
    <?
    if ($_FILES['userfile'])
    {
     
    ?>
     <form enctype="multipart/form-data"  method="POST">
       <input name="userfile" type="file" />
       <input type="submit" value="Upload" />
     </form>
     <?
    }
    else
    {
        
    $file_info pathinfo($_FILES['name']);
        if (
    in_array($file_info['extension'],$image_extensions) &&   
            
    in_array($_FILES['userfile']['type'],$image_mime_types))
           {
           
    //Valid Image.
           

           else
           {
            
    //not valid
            
    }
      
    }
    ?>
    i thought this would work. but it appears it doesnt work in all cases
    for instance if i get a zip file and rename the extension to .png it lets me upload it in ff but not ie? also i noticed the ie and ff give differnent MIME types for the same files... like ff: image/png msie: image/x-png
    is there a better way to do file verification besides MIME types? or am i just doing it wrong?

    any help appreciated thanks.

  2. #2
    SitePoint Evangelist Andrewaclt's Avatar
    Join Date
    Dec 2003
    Location
    Raleigh, NC
    Posts
    535
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would say get a valid list of MIME image types for Opera, FF and IE6,7 and manually hardcode it.

  3. #3
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Web browsers like to be different... well, IE likes to not follow the standards.

    Standards mean nothing on the internet, unfortunately!
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  4. #4
    SitePoint Zealot _theworks's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thankyou for your reply's this help's...

    but does anyone know where i can find a reliable source for all browser mime-types?

    edit

    dont worry i found one.

    http://www.webmaster-toolkit.com/mime-types.shtml
    Last edited by _theworks; Jan 22, 2006 at 23:51.

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if its available to you you can use the mime_content_type() function. That way you will get the same MIME type regardless of the browser.

  6. #6
    SitePoint Zealot _theworks's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, Codecfault, that would save me alot of effort. however i tried using it like this
    PHP Code:
    echo mime_content_type($_FILES['userfile']['tmp_name']); 
    just as a tester... but it said blah blah calling undefined function.. so i uncommented the line
    HTML Code:
    extension=php_mime_magic.dll
    in php.ini

    and now i just returns nothing. any idea why this is happening?

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here are two pages that will help you. Take note of the comments as well

    http://php.planetmirror.com/manual/e...ntent-type.php

    http://php.planetmirror.com/manual/e...mime-magic.php

  8. #8
    SitePoint Zealot _theworks's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey, thanks for the help so far

    but i am still confused as to why this is returning nothing.

    my php.ini has no lines like

    [mime_magic]
    mime_magic.magicfile = "/etc/apache/magic"
    infact the only instance of mime that was in php.ini was,
    extension=php_mime_magic.dll
    could this be my problem?

    server is running php5 but it is a rented server should i just contact my ISP and ask them why mime_content_type() is not working?

    thanks

  9. #9
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Possibly. I think if you want it they will have to recompile PHP with the enable-mime directive or whatever it happens to be. Ask tech support about the extension and kindly enquire as to if they could enable it for you.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •