Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP unlink() handling the exception

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) ??

like image 234
prakashchhetri Avatar asked Mar 10 '13 02:03

prakashchhetri


People also ask

What does the unlink () function do in PHP?

Unlink() function: The unlink() function is an inbuilt function in PHP which is used to delete a file. The filename of the file which has to be deleted is sent as a parameter and the function returns True on success and False on failure.

Can PHP delete files?

To delete a file in PHP, use the unlink function. Let's go through an example to see how it works. The first argument of the unlink function is a filename which you want to delete. The unlink function returns either TRUE or FALSE , depending on whether the delete operation was successful.

What are PHP exceptions?

An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions. Exceptions are a good way to stop a function when it comes across data that it cannot use.


2 Answers

NOTE: This probably won't work any longer. See Brian's comment

If you want to only suppress the error, you can do this:

@unlink('your_file_name'); 

Generally, in PHP, @ will suppress 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 :)

like image 100
goFrendiAsgard Avatar answered Oct 02 '22 21:10

goFrendiAsgard


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.

like image 23
prodigitalson Avatar answered Oct 02 '22 19:10

prodigitalson