joe92 Posted February 22, 2012 Share Posted February 22, 2012 Hi there, Does anybody know of a way to make php execute a redirect if there was a compiling error? Specifically for the means of redirecting during file uploads. Most likely to a script which would wait 5 seconds with a nice error message and redirect back. I had a look at set_error_handler but it specifically says: If errors occur before the script is executed (e.g. on file uploads) the custom error handler cannot be called since it is not registered at that time. So that's a no go it seems. If there is no way to do this with PHP, is there an FTP program that will upload the file you want with a ghost name during transfer, and then rename it and overwrite a conflicting file once the file has been 100% uploaded? That would also get around the problem I think. Thanks for any help, Joe Quote Link to comment https://forums.phpfreaks.com/topic/257529-php-compiling-error-redirect/ Share on other sites More sharing options...
AyKay47 Posted February 22, 2012 Share Posted February 22, 2012 A function from php.net towards the bottom of the set_error_handler documentation that detects PHP fatal errors: function shutdown(){ $isError = false; if ($error = error_get_last()){ switch($error['type']){ case E_ERROR: case E_CORE_ERROR: case E_COMPILE_ERROR: case E_USER_ERROR: $isError = true; break; } } if ($isError){ echo "Script execution halted ({$error['message']})"; } else { echo "Script completed"; } } this checks for several different fatal errors. You can adjust to your needs. Quote Link to comment https://forums.phpfreaks.com/topic/257529-php-compiling-error-redirect/#findComment-1319989 Share on other sites More sharing options...
joe92 Posted February 22, 2012 Author Share Posted February 22, 2012 Thanks for finding that. I've altered it slightly to suit my needs, and only redirect if I'm not in a test environment. Thought I'd post it here since it's actually quite handy. It captures the type of error as a number and redirects to an error script sending type in the GET. In the error php that number is then retrieved and placed into a table in the mysql database along with a time stamp. If there's a new one when I log in, then I am notified of it: <?php //Report all errors error_reporting(E_ALL); /** Define the shutdown error handling to redirect a user to a nice page if a fatal error is detected **/ function shutdown() { $sandboxing = false; //check if in test environment if(strstr($_SERVER['HTTP_HOST'],'127.0.0.1')) { $sandboxing = true; } $isError = false; if ($error = error_get_last()) { switch($error['type']) { //http://www.php.net/manual/en/errorfunc.constants.php //parsing errors case E_PARSE: $isError = true; $errNum = 1; break; //fatal errors case E_ERROR: $isError = true; $errNum = 2; break; //core php fatal erros case E_CORE_ERROR: $isError = true; $errNum = 3; break; //compiling errors case E_COMPILE_ERROR: $isError = true; $errNum = 4; break; //a user generated error, when you call trigger_error(); case E_USER_ERROR: $isError = true; $errNum = 5; break; } } //error detected, redirect user to error page, but only on actual site if($isError == true && $sandboxing == false) { header("location:http://www.mysite.com/error.php?$errNum"); exit(); } } //make sure the function is called upon reciept of an error set_error_handler('shutdown'); register_shutdown_function('shutdown'); This means I can define errors anywhere in the script, and be told if and when they happen. I like this feature. Might likely add in further details like location... Cheers, Joe Quote Link to comment https://forums.phpfreaks.com/topic/257529-php-compiling-error-redirect/#findComment-1320051 Share on other sites More sharing options...
joe92 Posted February 22, 2012 Author Share Posted February 22, 2012 Also, created a thread on FileZilla about ghost naming files during upload. Hopefully FileZilla will implement it. It's already got someone agreeing with me http://forum.filezilla-project.org/viewtopic.php?f=1&t=23809 Quote Link to comment https://forums.phpfreaks.com/topic/257529-php-compiling-error-redirect/#findComment-1320056 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.