Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle htaccess errors using php?

Tags:

php

.htaccess

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.

like image 522
AliN11 Avatar asked Jul 21 '26 02:07

AliN11


1 Answers

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
like image 144
Jason Butler Avatar answered Jul 22 '26 14:07

Jason Butler