i want to check the type of the file in php.i know that there is a function that can e used to do this thing but the thing is when u change the extension of the file, the function doesent help.
like i want to upload only text file.when i use that function it will return the type text/plain.this thing is ok but if i rename a jpg file to .txt then also this function return text/plain instead of image/jpg
can anyone tell me the sol

thanks in advance
Tushar

    Well, to obtain the file extension one would normally use either [man]pathinfo/man, or [man]explode/man or regex.

    Still, you might consider using [man]getimagesize/man. If the file is not a valid image, this function will return false and trigger a warning (which can be suppressed with @, of course).

      8 months later

      Hi everybody!

      I have exactly the same problem and I've tried both solutions (the image size and mime content type) but unfortunately none of them is working 🙁

      The image size returns invalid or something like that because it's a .txt file (I changed the .jpg or whatever into .txt manually to cheat) and php can't see it's an image disguised in text file.

      When I tried the mime content type, it keeps on saying it's an unknown function, I also tried an alternative function I found on the php.net:

      if ( ! function_exists ( 'mime_content_type ' ) )
      {
         function mime_content_type ( $f )
         {
             return system ( trim( 'file -bi ' . escapeshellarg ( $f ) ) ) ;
         }
      }

      but it does nothing :bemused: I read somewhere I need to change the php.ini settings to get the mime function to work, but (yes there's still one) I don't have access to this file... please help me!! 🙂

      Thanks

      Elena =)

        elena wrote:

        Hi everybody!

        I have exactly the same problem

        Thread-jacker!! :queasy:

        and I've tried both solutions (the image size and mime content type) but unfortunately none of them is working

        The image size returns invalid or something like that because it's a .txt file

        Well, that was part of laserlight's point. If [man]getimagesize/man returns false, at least you know you aren't dealing with an image (the original poster said he wanted to upload only text files ... so, um, are you sure you have the same problem?

        (I changed the .jpg or whatever into .txt manually to cheat) and php can't see it's an image disguised in text file.

        Need more information on that; it's certainly not true here.

        [292] Fri 21.Apr.2006 7:24:36                                                                                
        [kadmin@archangel][~] file scan.jpg scan.jpg: JPEG image data, JFIF standard 1.01 [293] Fri 21.Apr.2006 7:24:39 [kadmin@archangel][~] php -r "print_r (getimagesize('scan.jpg'));" Array ( [0] => 2312 [1] => 2224 [2] => 2 [3] => width="2312" height="2224" [bits] => 8 [channels] => 3 [mime] => image/jpeg ) [294] Fri 21.Apr.2006 7:24:48 [kadmin@archangel][~] mv scan.jpg scan.txt [295] Fri 21.Apr.2006 7:25:01 [kadmin@archangel][~] php -r "print_r (getimagesize('scan.txt'));" Array ( [0] => 2312 [1] => 2224 [2] => 2 [3] => width="2312" height="2224" [bits] => 8 [channels] => 3 [mime] => image/jpeg )

        When I tried the mime content type, it keeps on saying it's an unknown function, I also tried an alternative function I found on the php.net:

        if ( ! function_exists ( 'mime_content_type ' ) )
        {
           function mime_content_type ( $f )
           {
               return system ( trim( 'file -bi ' . escapeshellarg ( $f ) ) ) ;
           }
        }

        but it does nothing

        Does it work manually (see above)? You're aware that file is a Nixism, right? AFAIK there's no file(1) on Windows....

        I read somewhere I need to change the php.ini settings to get the mime function to work, but (yes there's still one) I don't have access to this file... please help me!!

        sigh. 🙁

        Really, I'd advise you to start your own thread, probably over in the "newbies" forum. 😉

        First, tell us about your exact problem --- what you expect to happen, and what actually happens that is unexpected. Be sure and tell us your operating system and server environment. Run phpinfo() and look for "mime" in the output. Show Us The Code. Find out if "file foo.txt" works from the command line.

        Your initial posting here is not bad, but not real auspicious, either. You hijacked a "resolved" thread from 8 months ago, you didn't show code, or really give many details about the problem at all. Try again? 🙂

          silly me :p !! you're asolutely right!! I was doing all wrong the whole image size thing !! :o

          actually I was testing the wrong file !! ooops

          getimagesize($_FILES['userfile']['name'])

          Warning: getimagesize(image_Small.txt): failed to open stream: No such file or directory in...

          instead of the 'tmp_name' cause it's not uploaded yet... oh my!! ok I admit it I had the solution right in front of my eyes but I didn't know how to use it!

          Thanks again! 🙂

            Write a Reply...