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) ??
If you want to only surpress the error, you can do this:
@unlink('your_file_name');
Generally, in php, @ will surpress any error.
The better way is minimize the error probability. You've say that one of error possibility is caused by non-exist file. If I were you, I'll do this:
if(file_exists('your_file_name')){
unlink('your_file_name');
}else{
echo 'file not found';
}
Good luck :)