PHP unlink()处理异常 [英] PHP unlink() handling the exception

查看:241
本文介绍了PHP unlink()处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我一直在想,是否可以正确处理 unlink()函数。我不想要 unlink()函数抛出一些讨厌的错误,如果它无法取消链接文件(可能是由于找不到文件)。



我尝试过像

  try {
unlink(secret / secret 。文本);
} catch(Exception $ e){
printwhoops!;
//甚至将它留空,所以没有显示
}

但是它不工作我不是PHP的专家。我搜索并发现这个异常处理代码在网络上的某个地方。但是,我记得我上学的时间,Java也是一样。所以应该有效我不知道代码有什么问题。



或者我可以使用if..else语句,如

  if(unlink($ file)){
//离开这里为空必须确保没有显示
} else {
//离开这里为空必须确保没有显示
}

但是这个代码也不工作。我在哪里做错?处理它的其他方法是什么?



可以通过使用错误报告(PHP)(生产和开发环境)操纵来隐藏错误吗? >

解决方案

unlink 不会抛出异常,在生成错误。在尝试调用 unlink 之前,正确的方法是检查文件是否存在。如果你只是担心没有错误输出,那么你应该只是关闭你在生产环境中应该总是做的 display_errors 。然后他们将被记录。



不要使用 @ 来抑制错误,这是很不可取的。 >


您可以更详细地描述 @




我不知道你的意思是什么。但是文档是 here 。至于为什么你不想使用它...那是因为那时你永远不知道代码不工作或有问题。即使代码仍然从功能角度出发,仍然有一个问题,这个问题可能会使别的东西在某个时候完全不起作用。如果你没有错误,你可能会浪费大量的时间调试。



更改日志级别或禁用显示错误,但您绝对不会完全禁止它们。


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.

这篇关于PHP unlink()处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆