8

I am using unlink on my PHP page. In some situations, Permission may be denied for deleting the directory. Instead of having

Warning: unlink(stuff/New folder) [function.unlink]: Permission denied in ... on line 30

show up on the rendered page, is there a way for me to do a "warning_get_last" that will capture the last given warning, so I can output it nicely? Or does error_get_last include these?

I know I can suppress the warnings with @unlink and that I can also check to see if unlink returns false, but I would like to know the error message that goes along with it if it does fail.

3 Answers 3

12

Use error_reporting(0) to not show the warning or any errors in the rendered page. It will still show up in your server error logs and you can still use error_get_last() to get the last error.

You can test it out with this:

error_reporting(0);
unlink('some file that does not exist'); // generates a warning
print_r(error_get_last());

EDITED: Please note that error_reporting(0) will affect subsequent code, so you'll want to set it back to the level you want after you're through the code where you want to suppress the error display.

Sign up to request clarification or add additional context in comments.

3 Comments

error_reporting(E_ERROR | E_WARNING) - will cause the error_get_last to display errors or warnings? I can use @ to stop the warnings from showing up. I just need a way to get the message from them.
Got the job done! The warnings are showing up with error_get_last. Thanks!
error_reporting(0): NEVER turn it off! Use ini_set('display_errors', 0); instead
1

You write set your own error handler, enable it just before the call, and revert back to normal afterwards.

Use set-error-handler to turn on the error handler, save to a global variable (that's the simplest - perhaps not most "correct") and show if there was an error. Or user error_get_last() (as suggeted by Trott).

function myErrorHandler($errno, $errstr, $errfile, $errline) {
    global $lastErrorString;
    $lastErrorString = $errstr;
    return false;
}

// Calling function:
global $lastErrorString;
$lastErrorString=false;
$old_error_handler = set_error_handler("myErrorHandler");
unlink($file);
restore_error_handler();
if ($lastErrorString !== false) {
    echo 'Went wrong: ' . $lastErrorString;
}

Comments

0

I fail to see why the following is any worse than the 2 other proposals; in fact I'd say it's better because it's shorter than Robbie's answer and doesn't have a global effect unlike Trott's answer:

$ok = @unlink(...);
if ($ok === FALSE)
    throw new Exception(error_get_last()['message']);

Output when run:

PHP Fatal error:  Uncaught exception 'Exception' with message 'unlink(foobar.txt): No such file or directory' in /Users/erik/code/test.php:5
Stack trace:
#0 {main}
  thrown in /Users/erik/code/test.php on line 5

Comments

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.