3

I've used ini_set('post_max_size',"2M") in my php script to limit uploads file size. it didn't worked (do you know why ? )

So i put these rules on my htaccess file :

php_value post_max_size 2M

php_value upload_max_filesize 2M

So when i upload a file larger than 2M , my php script shows this error :

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

How can i handle this error in an appropriate way ? (something like try catch).

Note : I wouldn't like to hide this error.

2
  • what values you see when you use phpinfo() ? Commented Dec 31, 2015 at 9:12
  • what values do you want ? upload_max_filesize 20M Commented Dec 31, 2015 at 9:13

1 Answer 1

1

In your upload script, have you tried:

if ($_FILES['upfile']['size'] > 2000000) {
    $error_message ="Exceeded filesize limit.";
}

Also there is try/catch in php for example:

try{
  //your upload script
}
catch(Exception $e){
    $error_message = "File did not upload: ". $e->getMessage();
}

From this you should be able to handle any errors how you want

Another solution is a custom error handler:

 set_error_handler("warning_handler", E_WARNING);

 //your upload script

 function warning_handler($errno, $errstr) { 
 // do someing with your error here
 }


 restore_error_handler(); // reinstates original error handling
Sign up to request clarification or add additional context in comments.

5 Comments

You can use this rule after file has been uploaded completely.
still getting that warning.
I don't know if this would work - however it may be worth a try... if you use @ before move_uploaded_file to surpress the warning, the try/catch statement may still catch it.
Another solution maybe to create a custom error handler.... ill up date my answer shortly
thanks. i tried this before. not works.i think the errors that throws by htaccess can't be managed by php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.