0

Following is the HTML :

<form id="request_form" method="post" enctype="multipart/form-data" action="print.php">
    <input type="text" class="form-control" name="stud_id" id="stud_id"/>
  <input type="file" name="student_image" id="student_image" accept="image/*" capture/>                  
  <button type="submit">Submit</button>
</form>

PHP code :

<?php
print_r($_POST); die;
print_r($_SERVER);
?>

Following are the file upload setting from my php.ini file:

upload_max_filesize = 10M

post_max_size = 10M

I'm getting the $_POST array blank if I upload image larger than 10 MB.

I want to access the values from $_POST array irrespective of size of uploaded image file. How should I get access to $_POST in this situation?

Please help me.

PHPLover
  • 1
  • 51
  • 158
  • 311
  • @Fred -ii-: It's not duplicate, there I was not able to access $_FILES array. Here I'm not able to access $_POST array. – PHPLover Dec 10 '14 at 14:11
  • instead of saying: '10M' change it to '10MB' the post array is empty because you're uploading a file greater than 10MB.. which results in the file not being processed. – Gerwin Dec 10 '14 at 14:12
  • `post_max_size` is 10M. you're uploading more than 10meg. so obviously your image data completely filled up the post body with no more space allowed for your other form fields. "this cup is full. I poured in more water. why didn't the level go up, and why is there a puddle now?" – Marc B Dec 10 '14 at 14:12
  • @Gerwin:Yes I know that but what about the $_POST data. How should I get that data? – PHPLover Dec 10 '14 at 14:13
  • $_GET on the array could work? that way you would get all the data in the array? – Gerwin Dec 10 '14 at 14:14
  • Well, you should have posted that in your other question afterwards. I'll reopen, but you could have just added to your other question. There is no need to have the other question then, delete it. – Funk Forty Niner Dec 10 '14 at 14:14
  • @MarcB:How should I validate the data from $_POST array? If user has filled in the field and uploads a largre image then I should provide him the appropriate message. That I'm not able to do now. – PHPLover Dec 10 '14 at 14:15
  • `$_FILES` is just `$_POST`data that has been handled a special way for you so you don't have to process files yourself. If the file is too big, then `$_POST` will break completely. – Niet the Dark Absol Dec 10 '14 at 14:16
  • @Gerwin:No $_GET can't work. I have to access the filled in values at any cost irrespective of the size of the uploaded image. – PHPLover Dec 10 '14 at 14:16
  • @NiettheDarkAbsol : Ok, Thanks for sharing the knowledge. But is there any other way I can access the data filled in by user when he uploads image larger than 10 MB? – PHPLover Dec 10 '14 at 14:17
  • @Gerwin `$_GET` won't work, since uploading files requires a POST method. However, OP could use Ajax. – Funk Forty Niner Dec 10 '14 at 14:20
  • you need to check `$_FILES['student_image']['error']` **FIRST**. If the upload failed for any reason whatsoever, the error parameter will say so (and why). In your case, the file is maxing out the POST max limit because it's FIRST in the form. Browser tend to submit fields in the order they're encountered in the html. If you want your other fields to appear in $_POST, even if the file is too large, then put the other fields before the file input. – Marc B Dec 10 '14 at 14:22
  • Did you not see the link I gave you in [your other question](http://stackoverflow.com/questions/27401824/getting-files-array-empty-for-some-specific-image-files?lq=1)? => http://stackoverflow.com/q/18567861/ – Funk Forty Niner Dec 10 '14 at 14:24
  • @MarcB : Sorry I didn't get your point. Can you please make change to my code in order to implement the thing you are saying. – PHPLover Dec 10 '14 at 14:25
  • @Fred-ii-:Yes I saw and implemented the same but now the issue is with $_POST data. That question has nothing about $_POST data. – PHPLover Dec 10 '14 at 14:27
  • It's really quite simple. If the file exceeds the allowed size, just as this answer http://stackoverflow.com/a/18568346/ on that same page, then assign your POST variable in there instead. You will still be able to access it. I.e.: `if($_FILES['fileupload']['error'] === UPLOAD_ERR_INI_SIZE) { // Handle the error echo 'Your file is too large.'; $student_id = $_POST['stud_id']; echo "
    " . $student_id;` - or using the one in http://stackoverflow.com/a/18568104/ while applying the same logic.
    – Funk Forty Niner Dec 10 '14 at 14:29
  • *Plus,* if you want to avoid the user going through the entire upload process for nothing, because the file is too large, then you will need to implement some JS with a listener in order to pre-fetch the file's size. Now that, is another game entirely. PHP doesn't "pre-fetch" a file's size. – Funk Forty Niner Dec 10 '14 at 14:43
  • @Fred-ii- `$_GET` can be sent in POST requests. – Marek Dec 10 '14 at 14:58

1 Answers1

0

Basically this is what's happening - this is not 100% accurate, but it's just a basic representation for illustrative purposes.

You've got a form, with some number of input fields:

<input type="text" name="foo" value="bar" />
<input type="file" name="somefile" />
<input type="text" name="baz" value="qux" />

When the form is submitted, browsers will convert the form fields/values into a string and send that string to the server:

foo=bar&somefile=contents of file here&baz=bux

0000000001111111111222222222233333333334444444
1234567890123456789012345678901234567890123456  -- character position

Let's say your post_max_size is 30 chars. That means PHP will process only this:

foo=bar&somefile=contents of f

So you'd end up with:

$_POST['foo'] = 'bar'
$_FILES['somefile'] => 'contents of f' + error = 3 - partial upload

Note that baz = qux is nowhere to be seen - it was past the end of the chunk of data PHP did process, and is effectively lost.

if you rearranged your form so that the various text fields come first:

<input type="text" name="foo" value="bar" />
<input type="text" name="baz" value="qux" />
<input type="file" name="somefile" />

then on submission you'd get:

foo=bar&baz=qux&somefile=contents of file here

0000000001111111111222222222233333333334444444
1234567890123456789012345678901234567890123456

and with the same 30 char limit on POST size:

foo=bar&baz=qux&somefile=conte

producing

$_POST['foo'] = 'bar';
$_POST['baz'] = 'qux';
$_FILES['somefile'] = 'conte' + error = 3

The reality is more complex than this - a file upload will NOT appear in the posted data in this exact manner. A file upload uses a different body construction and form encoding semantics, but the end result is the same: You performed a POST that exceeded the maximum size PHP allows. Part of that POST got truncated, causing some of your form data to be lost.

Marc B
  • 356,200
  • 43
  • 426
  • 500