Jump to content

post_max_size and upload_max_filesize, how to control them in a php page not a php ini file


Recommended Posts

As we know ,

A post_max_size used to limit the size of posted data via POST method

while 

the upload_max_filesize used to limit the size of file to be uploaded

 

I assume.

post_max_size = 8m

upload_max_filesize = 2m

 

When i limit the user to only upload 100kb file, i say

<pre>

  if (!empty($_FILES["myfile"]["tmp_name"]) && ($_FILES["myfile"]["size"] > 100000)) {
       echo "the file is too large";
    }

</pre>

 

This message occur even when the file is greater than 8 m

 

The problem come when the file is greater than 10m the warning message for post content is on the page is

 

Warning: POST Content-Length of 9767236 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

 

What i want?

I want to create my own warning as an error

 

ie

<pre>

  if (post_max_size > 18388608)) {
       echo "the posted content exceed our server limit ";
    }

</pre>

 

NB: some developers says

we have to adjust the post_max_size i.e 200M what if user upload the file with 210M, the warning message will come back again

 

Warning: POST Content-Length of 9767236 bytes exceeds the limit of 8388608 bytes in Unknown on line 0

 

In short i want to give a normal user a meaningful warning

 

Link to post
Share on other sites

when you exceed the post_max_size setting, both the $_POST and $_FILES arrays are empty, so, logic trying to use those values won't have anything to test.

your post method form processing code should first test if a post method form was submitted. you can then compare the content-length (see $_SERVER['CONTENT_LENGTH']) value (which is in bytes) with the post_max_size (see ini_get('post_max_size') ) setting (which used to be in whatever units the setting is in  - bytes or K, M, G short-notation, which may now be (unconfirmed) always in bytes due to the addition of the new Warning message for this condition) to produce your own error message. when on a live/public server you should set php's display_errors setting to OFF and set log_errors to ON to cause php error messages, which would include that new Warning message, to be logged instead of displayed.

 

  • Like 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.