$_POST data returns empty when headers are > POST_MAX_SIZE
<p>Hopefully someone here might have an answer to my question.</p>

<p>I have a basic form that contains simple fields, like name, number, email address etc and 1 file upload field.</p>

<p>I am trying to add some validation into my script that detects if the file is too large and then rejects the user back to the form to select/upload a smaller file.</p>

<p>My problem is, if a user selects a file that is bigger than my validation file size rule and larger than php.ini POST_MAX_SIZE/UPLOAD_MAX_FILESIZE and pushes submit, then PHP seems to try process the form only to fail on the POST_MAX_SIZE settings and then clears the entire $_POST array and returns nothing back to the form.</p>

<p>Is there a way around this? Surely if someone uploads something > than the max size configured in the php.ini then you can still get the rest of the $_POST data???</p>

<p>Here is my code.</p>

<pre><code>&lt;?php

   function validEmail($email)
    {
       $isValid = true;
       $atIndex = strrpos($email, "@");
       if (is_bool($atIndex) &amp;&amp; !$atIndex)
       {
          $isValid = false;
       } else {
          $domain = substr($email, $atIndex+1);
          $local = substr($email, 0, $atIndex);
          $localLen = strlen($local);
          $domainLen = strlen($domain);

          if ($localLen &lt; 1 || $localLen &gt; 64)
          {
             // local part length exceeded
             $isValid = false;
          }
          else if ($domainLen &lt; 1 || $domainLen &gt; 255)
          {
             // domain part length exceeded
             $isValid = false;
          }
          else if ($local[0] == '.' || $local[$localLen-1] == '.')
          {
             // local part starts or ends with '.'
             $isValid = false;
          }
          else if (preg_match('/\\.\\./', $local))
          {
             // local part has two consecutive dots
             $isValid = false;
          }
          else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
          {
             // character not valid in domain part
             $isValid = false;
          }
          else if (preg_match('/\\.\\./', $domain))
          {
             // domain part has two consecutive dots
             $isValid = false;
          }
          else if
          (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&amp;`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
          {
             // character not valid in local part unless 
             // local part is quoted
             if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local)))
             {
                $isValid = false;
             }
          }

       }
       return $isValid;
    }

        //setup post variables
        @$name = htmlspecialchars(trim($_REQUEST['name'])); 
        @$emailCheck = htmlspecialchars(trim($_REQUEST['email']));
        @$organisation = htmlspecialchars(trim($_REQUEST['organisation']));
        @$title = htmlspecialchars(trim($_REQUEST['title']));
        @$phone = htmlspecialchars(trim($_REQUEST['phone']));
        @$location = htmlspecialchars(trim($_REQUEST['location']));
        @$description = htmlspecialchars(trim($_REQUEST['description']));
        @$fileError = 0;
        @$phoneError = "";

        //setup file upload handler
        $target_path = 'uploads/';
        $filename =  basename( @$_FILES['uploadedfile']['name']);
        $max_size = 8000000; // maximum file size (8mb in bytes) NB: php.ini max filesize upload is 10MB on test environment.
        $allowed_filetypes = Array(".pdf", ".doc", ".zip", ".txt", ".xls", ".docx", ".csv", ".rtf"); //put extensions in here that should be uploaded only.
        $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

        if(!is_writable($target_path)) die('You cannot upload to the specified directory, please CHMOD it to 777.'); //Check if we can upload to the specified upload folder.


        //display form function
        function displayForm($name, $emailCheck, $organisation, $phone, $title, $location, $description, $phoneError, $allowed_filetypes, $ext, $filename, $fileError)
        {
          //make $emailCheck global so function can get value from global scope.
          global $emailCheck;
          global $max_size;



          echo  '&lt;form action="geodetic_form.php" method="post" name="contact" id="contact" enctype="multipart/form-data"&gt;'."\n".
                '&lt;fieldset&gt;'."\n".'&lt;div&gt;'."\n";

          //name        
          echo '&lt;label for="name"&gt;&lt;span class="mandatory"&gt;*&lt;/span&gt;Your name:&lt;/label&gt;'."\n".
                '&lt;input type="text" name="name" id="name" class="inputText required" value="'. $name .'" /&gt;'."\n";

                //check if name field is filled out
                if (isset($_REQUEST['submit']) &amp;&amp; empty($name)) 
                {        
                  echo '&lt;label for="name" class="error"&gt;Please enter your name.&lt;/label&gt;'."\n";
                }

           echo '&lt;/div&gt;'."\n". '&lt;div&gt;'."\n";

           //Email     
           echo '&lt;label for="email"&gt;&lt;span class="mandatory"&gt;*&lt;/span&gt;Your email:&lt;/label&gt;'."\n".
                '&lt;input type="text" name="email" id="email" class="inputText required email" value="'. $emailCheck .'" /&gt;'."\n";

               // check if email field is filled out and proper format   
                if (isset($_REQUEST['submit']) &amp;&amp; validEmail($emailCheck) == false)
                {
                  echo '&lt;label for="email" class="error"&gt;Invalid email address entered.&lt;/label&gt;'."\n";
                }

           echo '&lt;/div&gt;'."\n". '&lt;div&gt;'."\n";

           //organisation     
           echo '&lt;label for="phone"&gt;Organisation:&lt;/label&gt;'."\n".
                '&lt;input type="text" name="organisation" id="organisation" class="inputText" value="'. $organisation .'" /&gt;'."\n";
           echo '&lt;/div&gt;'."\n". '&lt;/fieldset&gt;'."\n".'&lt;fieldset&gt;'. "\n" . '&lt;div&gt;'."\n";

           //title     
           echo '&lt;label for="phone"&gt;Title:&lt;/label&gt;'."\n".
                '&lt;input type="text" name="title" id="title" class="inputText" value="'. $title .'" /&gt;'."\n";        
           echo '&lt;/div&gt;'."\n". '&lt;/fieldset&gt;'."\n".'&lt;fieldset&gt;'. "\n" . '&lt;div&gt;'."\n";

          //phone     
           echo '&lt;label for="phone"&gt;&lt;span class="mandatory"&gt;*&lt;/span&gt;Phone &lt;br /&gt;&lt;span class="small"&gt;(include area code)&lt;/span&gt;:&lt;/label&gt;'."\n".
                '&lt;input type="text" name="phone" id="phone" class="inputText required" value="'. $phone .'" /&gt;'."\n";       

           // check if phone field is filled out that it has numbers and not characters
           if (isset($_REQUEST['submit']) &amp;&amp; $phoneError == "true" &amp;&amp; empty($phone)) echo '&lt;label for="email" class="error"&gt;Please enter a valid phone number.&lt;/label&gt;'."\n";       

           echo '&lt;/div&gt;'."\n". '&lt;/fieldset&gt;'."\n".'&lt;fieldset&gt;'. "\n" . '&lt;div&gt;'."\n";

            //Location     
            echo '&lt;label class="location" for="location"&gt;&lt;span class="mandatory"&gt;*&lt;/span&gt;Location:&lt;/label&gt;'."\n".
                 '&lt;textarea name="location" id="location" class="required"&gt;'. $location .'&lt;/textarea&gt;'."\n";

            //check if message field is filled out
            if (isset($_REQUEST['submit']) &amp;&amp; empty($_REQUEST['location'])) echo '&lt;label for="location" class="error"&gt;This field is required.&lt;/label&gt;'."\n";

            echo '&lt;/div&gt;'."\n". '&lt;/fieldset&gt;'."\n".'&lt;fieldset&gt;'. "\n" . '&lt;div&gt;'."\n";

           //description     
           echo '&lt;label class="description" for="description"&gt;Description:&lt;/label&gt;'."\n".
                '&lt;textarea name="description" id="queryComments"&gt;'. $description .'&lt;/textarea&gt;'."\n";               
           echo '&lt;/div&gt;'."\n". '&lt;/fieldset&gt;'."\n".'&lt;fieldset&gt;'. "\n" . '&lt;div&gt;'."\n";

          //file upload
           echo '&lt;label class="uploadedfile" for="uploadedfile"&gt;File:&lt;/label&gt;'."\n".
                '&lt;input type="file" name="uploadedfile" id="uploadedfile" value="'. $filename .'" /&gt;'."\n";

           // Check if the filetype is allowed, if not DIE and inform the user.   
           switch ($fileError)
           {
            case "1":
                echo '&lt;label for="uploadedfile" class="error"&gt;The file you attempted to upload is not allowed.&lt;/label&gt;';
            break;

            case "2":
                echo '&lt;label for="uploadedfile" class="error"&gt;The file you attempted to upload is too large.&lt;/label&gt;';
            break;
           }   
           echo '&lt;/div&gt;'."\n". '&lt;/fieldset&gt;';

            //end of form
            echo '&lt;div class="submit"&gt;&lt;input type="submit" name="submit" value="Submit" id="submit"  /&gt;&lt;/div&gt;'.
                 '&lt;div class="clear"&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;/div&gt;';
        } //end function

        //setup error validations
        if (isset($_REQUEST['submit']) &amp;&amp; !empty($_REQUEST['phone']) &amp;&amp; !is_numeric($_REQUEST['phone'])) $phoneError = "true";
        if (isset($_REQUEST['submit']) &amp;&amp; $_FILES['uploadedfile']['error'] != 4 &amp;&amp; !in_array($ext, $allowed_filetypes)) $fileError = 1;
        if (isset($_REQUEST['submit']) &amp;&amp; $_FILES["uploadedfile"]["size"] &gt; $max_size) $fileError = 2; echo "this condition " . $fileError; 

        $POST_MAX_SIZE = ini_get('post_max_size');
        $mul = substr($POST_MAX_SIZE, -1);

        $mul = ($mul == 'M' ? 1048576 : ($mul == 'K' ? 1024 : ($mul == 'G' ? 1073741824 : 1)));
        if ($_SERVER['CONTENT_LENGTH'] &gt; $mul*(int)$POST_MAX_SIZE &amp;&amp; $POST_MAX_SIZE) echo "too big!!";
        echo $POST_MAX_SIZE;


        if(empty($name) || empty($phone) || empty($location) || validEmail($emailCheck) == false || $phoneError == "true" || $fileError != 0)
        {
            displayForm($name, $emailCheck, $organisation, $phone, $title, $location, $description, $phoneError, $allowed_filetypes, $ext, $filename, $fileError);
          echo $fileError;
          echo "max size is: " .$max_size;
          echo "and file size is: " .  $_FILES["uploadedfile"]["size"];
          exit;
        } else {

            //copy file from temp to upload directory
            $path_of_uploaded_file = $target_path . $filename;
            $tmp_path = $_FILES["uploadedfile"]["tmp_name"];
            echo $tmp_path;
            echo "and file size is: " .  filesize($_FILES["uploadedfile"]["tmp_name"]);
            exit;
            if(is_uploaded_file($tmp_path))
            {
              if(!copy($tmp_path,$path_of_uploaded_file))
              {
                echo 'error while copying the uploaded file';
              }
            }

        //test debug stuff
            echo "sending email...";
            exit;


        }
        ?&gt;
</code></pre>

<p>PHP is returning this error in the log:
[29-Apr-2010 10:32:47] PHP Warning:  POST Content-Length of 57885895 bytes exceeds the limit of 10485760 bytes in Unknown on line 0</p>

<p>Excuse all the debug stuff :)</p>

<p>FTR, I am running PHP 5.1.2 on IIS.</p>
