I have a form with a small number (up to 10) of inputs. One of those inputs is a file upload input. The form layout is as follows:
<form method="post" ...>
<input type="hidden" name="input_no_1" value="value_no_1">
<input type="file" name="input_no_2">
<input type="hidden" name="input_no_3" value="value_no_3">
<button type="submit">send</button>
99.9% of the time this form works fine and I get all the data on the PHP side. However, every once in a while, the POST data gets truncated, and the $_POST variable contains only this:
input_no_1 => value_no_1
The file input and every other input that follows the file input are missing.
This error happens almost exclusively with user agents that contain "Mobile Safari". I suspect that mobile phone users could be using a less reliable network connection and as a result, their request gets aborted and PHP doesn't receive the entire uploaded data.
In my application, I need to know whether the data in the $_POST array is complete or not. I was expecting that if the HTTP server doesn't receive the entire request, I wouldn't even see it from the PHP side.
I am using apache/fpm. The upload_max_filesize and post_max_size are both set to 2G and are nowhere near the size of the uploaded files.
The question: what is the best way to handle incomplete POST data in PHP? Is there a way to prevent these requests from reaching my application, and if not, is there a way to know whether the posted data is complete? I prefer not to add any fields at the end of the form just to find out whether the request is complete or not :)
It sounds like this is a case of needing some server side form validation. Assuming the above fields are all required, something like the following might work for you:
if (!isset($_POST['input_no_1'], $_POST['input_no_2'], $_FILES['input_no_2'])) {
die('Form not filled completely');
} else {
//process form
}