Check the file extension

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maheswaran
    New Member
    • Mar 2007
    • 190

    Check the file extension

    Hi,

    I want to check the extension of uploaded file.

    I upload the bmp image using file property. Now i am going to edit the file upload text as bmp file into gif...

    example : i upload c:/sample.bmp now i manually edit the file input box as c:/sample.gif and i upload this one.

    Now i want to check the orignial file ext.. is there any way in javascript or php.....
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    What do you mean by: "manually edit the file input box"?
    What are you doing with the files after you upload them?

    You need to explain this a lot better.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, maheswaran.

      Instead of validating the extension, try validating the mime-type of the file itself. The Fileinfo extension works very well for this (http://php.net/finfo_file).

      Comment

      • chelvan
        New Member
        • Aug 2008
        • 90

        #4
        hi

        use the following for your if condition

        $_FILES["file"]["type"];



        regards

        chel-1

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by chelvan
          hi

          use the following for your if condition

          $_FILES["file"]["type"];



          regards

          chel-1
          It's not good to rely on that tho. This is supplied by the client, so it can be manipulated.

          From the manual:
          Originally posted by www.php.net
          $_FILES['userfile']['type']
          The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.

          Comment

          • maheswaran
            New Member
            • Mar 2007
            • 190

            #6
            Hi Atli,

            Manually do means..

            First i upload a file...

            Now input box have <input type="file" name="uploadto" value="c:/sample.gif">

            Then am going to change the value gif as jpg <input type="file" name="uploadto" value="c:/sample.jp..."


            After change am going to upload..so am have error... In that case i want to check the file extension or file validation....

            Comment

            • chelvan
              New Member
              • Aug 2008
              • 90

              #7
              Originally posted by Atli
              It's not good to rely on that tho. This is supplied by the client, so it can be manipulated.

              From the manual:
              hi
              thanks for your information.

              find out the "/" position then using substr() the user can check the extension. this is the way i thought.

              so......... the above is wrong no?


              thanks
              chel-1

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                Originally posted by maheswaran
                Hi Atli,

                Manually do means..

                First i upload a file...

                Now input box have <input type="file" name="uploadto" value="c:/sample.gif">

                Then am going to change the value gif as jpg <input type="file" name="uploadto" value="c:/sample.jp..."
                How do you upload that first file?

                Setting the "value" attribute on a <input type="file"> element should have no effect. It will simply be ignored. You will actually have to hit the "browse" button and manually select a file before submitting the form.

                Originally posted by maheswaran
                After change am going to upload..so am have error... In that case i want to check the file extension or file validation....
                What error are you getting?
                Can we see the code you are using?

                It's very hard to understand the problem without the code, especially with such a limited description of what it is you are doing.

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Originally posted by chelvan
                  hi
                  thanks for your information.

                  find out the "/" position then using substr() the user can check the extension. this is the way i thought.

                  so......... the above is wrong no?


                  thanks
                  chel-1
                  The file extension is also a very unreliable way to determine the type of a file.
                  I could simply add a ".jpeg" extension to any file and your script would accept it as a JPEG image.

                  Using the method suggested by pbmods is much more reliable. It actually checks the file, not just the file name or the data feed to you by the client.

                  Comment

                  • chelvan
                    New Member
                    • Aug 2008
                    • 90

                    #10
                    Originally posted by Atli
                    The file extension is also a very unreliable way to determine the type of a file.
                    I could simply add a ".jpeg" extension to any file and your script would accept it as a JPEG image.

                    Using the method suggested by pbmods is much more reliable. It actually checks the file, not just the file name or the data feed to you by the client.
                    hi atli !
                    thanks for your information
                    chel-1

                    Comment

                    • maheswaran
                      New Member
                      • Mar 2007
                      • 190

                      #11
                      yes pbmods code is seems to be fine.Thanks

                      Comment

                      Working...