www.webdeveloper.com
Results 1 to 6 of 6

Thread: Check to see if file uploaded is actually a picture

  1. #1
    Join Date
    Apr 2013
    Posts
    23

    Check to see if file uploaded is actually a picture

    <form enctype="multipart/form-data" method="POST" action="ads.php" />
    Name:<input type="text" name ="name" /><br />
    Book:<input type="text" name ="book" /><br />
    Subject:<input type="text" name ="subject" /><br />
    Price (CAN):<input type="text" name ="price" /><br />
    Description: <br /><textarea cols="40" rows="5" name="description">

    </textarea><br />
    Click to upload:<input type="file" name="pix" /><br />
    <input type="submit" name="Upload" value="Upload Picture" />
    </form>

    I want this form to only submit if the pictures are either gif, jpeg, or png.

    I want to have protection against blank entries, incomplete entries. duplicate entries

    Would that require an IF statement? Or something more advanced like JQuery / AJAX?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,247
    getimagesize() can help you with this.

    I don't know if there is any way via JavaScript to validate before submitting to the server, but you can definitely check it on the server side -- and you need to anyway in case somebody sidesteps your client-side validation.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2011
    Posts
    1,138
    I have a vague recollection of trying to do some validation via JavaScript, but even though I was only trying to validate the filename extension in the <form> (which is unreliable in itself) I ran into security issues. You *might* be able to find a Java applet that would do the job on the client side, but the easiest method is as NogDog suggests - do it on the server.
    Rick Trethewey
    Rainbo Design

  4. #4
    Join Date
    Apr 2013
    Posts
    23

    Re:

    So I let the user submit the text and picture?

    Then once they send it I double check before uploading it into the database?

    Some websites will not let you even click the button to submit unless the information is correct, and I'm wondering how they do it...

    Your advice is much appreciated NogDog, but i'm still confused.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,247
    Let's assume that the file upload field in your HTML form is named "the_file":
    PHP Code:
    <?php

    if(empty($_FILES['the_file']['tmp_name'])
       or ! 
    isValidImageFile($_FILES['the_file']['tmp_name']))

        
    // output error, return to form page, or whatever you want to do
    }
    else {
        
    // go ahead and continue processing the submitted file
    }

    /**
     * Check if a file is JPEG, GIF, or PNG
     * @return Boolean
     * @param  string  image file path-name
     */
    function isValidImageFile($file)
    {
        
    $result false;
        if(
    file_exists($file) and is_readable($file)) {
            if((
    $imgData getimagesize($_FILES['the_file']['tmp_name'])) != false) {
                switch(
    $imgData[2]) {
                    case 
    IMG_GIF:
                    case 
    IMG_JPG:
                    case 
    IMG_PNG:
                        
    $result true;
                        break;
                    default:
                        
    $result false;
                }
            }
        }
        return 
    $result;
    }
    (I didn't test this, so not 100% guaranteed to be error-free.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Apr 2013
    Posts
    23

    Re:

    Wow thanks. I'll test it soon and let you know.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles