Well, I have been wondering if I can handle the unlink()
function properly. I dont want the unlink()
function to throw some nasty error if it is unable to unlink the file (may be due to the File not found).
I tried something like
try {
unlink("secret/secret.txt");
} catch(Exception $e) {
print "whoops!";
//or even leaving it empty so nothing is displayed
}
But it is not working. I am no expert in PHP. I searched and found this exception handling code somewhere in the web. But as I can remember my school days, the same was used for Java. SO it should have worked. I dont know whats wrong with the code.
Or can I simply use a if..else statement like
if(unlink($file)){
//leaving here empty must ensure that nothing is displayed
}else{
//leaving here empty must ensure that nothing is displayed
}
But this code isnt working either. Where am I doing the mistake? What are the other ways to handle it properly?
Can the errors be hidden by manipulating with the error reporting (PHP) (Production and Development environment) ??
unlink
doesn't throw exceptions, in generates errors. The proper way to do this is check that the file exists before trying to call unlink
on it. If you are merely worried about not having the errors output then you should just turn off display_errors
which you should always do in a production environment anyway. Then they will just be logged.
Do not suppress errors with the @
, its rarely advisable.
Can you be more descriptive about
@
Im not sure what you mean exactly. But the documentation is here. As far as why you don't want to use it... That is because then you never know that code isn't working or is problematic. Even if the code still works from a functional perspective it's still got an issue and that issue could potentially make something else completely not work at some point. If you never have the error you'll probably waste a lot of time debugging.
Its fine to change your log level or disable the display of errors, but you never want to completely suppress them.