redirect warning message to string variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
area5one
Forum Newbie
Posts: 2
Joined: Fri Jul 03, 2009 3:30 am

redirect warning message to string variable

Post by area5one »

Hi guys,

I have

Code: Select all

 
rmdir("non_existent_dir");
 
Output:
Warning: rmdir(non_existent_dir) [function.rmdir]: No such file or directory in C:\wwwroot\test\index.php on line 59

Any way of getting that string into a variable?
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: redirect warning message to string variable

Post by Eric! »

You can check to see if it is a directory or not and use error handling.

Code: Select all

<?php
if (!is_dir('examples')) {
    echo 'Does not exist';
}
else {
 if (rmdir('examples')==0) echo 'Failed to remove directory';
  else echo 'Directory removed.';
}
?>
Edit: I see that you can from next post. You have to set a callback function to process the error and then you should probably restore the handler unless you want to keep your function as the default way to show errors/warnings.
Last edited by Eric! on Fri Jul 03, 2009 7:57 am, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: redirect warning message to string variable

Post by VladSun »

There are 10 types of people in this world, those who understand binary and those who don't
area5one
Forum Newbie
Posts: 2
Joined: Fri Jul 03, 2009 3:30 am

Re: redirect warning message to string variable

Post by area5one »

Thanks for the link VladSun

Here is an example of printing the error message in a different colour

Code: Select all

function myErrorHandler($errno, $errstr, $errfile, $errline)
{  echo "<span style='color: red'>".$errstr."</span>";
}
 
set_error_handler("myErrorHandler");
rmdir("non_existent_dir");
Post Reply