Announcement

Collapse
No announcement yet.

How do you validate files?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do you validate files?

    Say that I have an upload form on one of my web pages, how do I validates that all the files uploaded are images files only?(gif & jpg) And how do I specifly a maxium size in kbyte?
    Thanks!

  • #2
    Originally posted by La La Lol
    Say that I have an upload form on one of my web pages, how do I validates that all the files uploaded are images files only?(gif & jpg) And how do I specifly a maxium size in kbyte?
    Thanks!
    Well, getimagesize() has always done it for me. Just run a checker on the image, and then use, say, $img[2] for the type (index 2 is the type). Not sure about kb size, but a read might do it... Poke around, and here's the getimagesize() page: http://www.php.net/manual/en/function.getimagesize.php

    Comment


    • #3
      So what would the code look like say I only want gif and jpeg file with max. size 500 x 500? And I don't want people to upload files other than gif and jpeg... for example if people are trying to upload a html doc. how do I reject them but let them have jpeg or gif?
      Thanks alot!

      Comment


      • #4
        Well, check the file resource (that was stored in temp data) by using something like this:
        PHP Code:
        $imgtest getimagesize($img);
        if ((
        $imgtest[2] == 1) || ($imgtest[2] == 2))
            echo 
        "Image is a JPEG or GIF";
        if ((
        $imgtest[0] < 500) || ($imgtest[1] < 500))
            echo 
        "Image is smaller then 500x500px"
        Make sure to check the direction of those Less Then, cuz I get them backwards most times..
        Last edited by SiGmA_X; Thu 8th May '03, 11:12am.

        Comment


        • #5
          Originally posted by SiGmA_X
          Make sure to check the direction of those Less Then, cuz I get them backwards most times..
          They should be the other way round

          == equals
          === identical
          != not equal
          !== not identical
          <> not equal
          < less than
          > greater than
          <= less than or equal to
          >= greater than or equal to

          Sean
          SitePoint Advisor (seanf)
          http://sitepointforums.com
          Harry Potter

          Comment


          • #6
            Originally posted by seanf
            They should be the other way round

            == equals
            === identical
            != not equal
            !== not identical
            <> not equal
            < less than
            > greater than
            <= less than or equal to
            >= greater than or equal to

            Sean
            Thanks I test everything (Other then vB hacks because they normally give DB errors locally, or the the live server if I am doing both..) locally, so I can see if it gives a error due to my un-logic

            Comment


            • #7
              So shoulde I use the normal equals == or identical equals === ?

              Comment


              • #8
                It depends on what you need. Do you just want to find out if two values are the same, or do you want to know if they are the same and the same type? I wrote a mini-tutorial here with an example of each, which might help you decide

                Sean
                SitePoint Advisor (seanf)
                http://sitepointforums.com
                Harry Potter

                Comment


                • #9
                  I have got another question here... I just want to allow people to upload images on my site, now it works fine but I MUST set the pemission to 777... is that very dangerous? I mean, peole can easily write another PHP on their website to massively upload files and viruses to that folder right? Is there anyway that I can make it so only my script can upload files to that folder? Thank you!

                  Comment


                  • #10
                    Setting permissions on a file/folder to 777 should just allow any scripts on your server to edit or view it. That doesn't mean that someone can remotely upload files.
                    MTG Salvation.com | Forums

                    Comment


                    • #11
                      Then why is that people saying it is not good setting it to 777?

                      Comment


                      • #12
                        Originally posted by La La Lol
                        Then why is that people saying it is not good setting it to 777?
                        i guess cuz ppl could upload files to your folder which are on the same server (unless safe_mode is on). dunno if that's really the "problem" with the 777 stuff, but it would be a very good reason to not use 777 on shared / paid hosts. imagine you've got 500 mb webspace and someone with a small account like 50 mb could upload 100 mb files to your folder by knowing the absolute path, but like said before dunno if that's the reason.

                        Comment

                        Working...
                        X