SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Right method to begin server side validation for file uploads

    What is the right way to begin validation for data sent by a multipart/form form. I'll be receiving a file and 1 text identifier from a hidden field.

    I'm not asking how to check for file type and size and all else. I can do that :-) What I'm asking is: in a normal form I usually do this:

    Code:
    <?php
    
    If ($_POST)
    {
    
    if (isset($_POST['myField]) && $_POST['myField'] == 1)
    {
    //Proceed with the rest
    }else{
    echo 'Error';
    }
    }
    else
    {
    echo 'Some Error';
    }
    
    ?>
    What's the best way to do it when it come to multipart/form data? How do u pros / experienced guys do it?

    Thanks

  2. #2
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    It depends on the file type you are uploading and any restrictions you are imposing. Look up the following PHP methods for more information:

    is_uploaded_file
    move_uploaded_file
    mime_content_type (if you only accept gif, you want to catch and fail on bmp right?)
    getimagesize (to check upload size limits if using images)
    filesize (if not an image)

    Between these, and basic checks, you should be able to determine validity of your upload.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    To check first off if the upload happened without incident check the error code is not 0.
    PHP Code:
    <?php 
    if($_FILES['userfile']['error'] > 0) { 
      
    // handle the error 
      // redirect or whatever


    // proceed with other checks, move file etc
    ?>
    Upload error codes


Tags for this Thread

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
  •