在文件未上传的 PHP 中尝试 catch 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/933081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
try catch statement in PHP where the file does not upload
提问by Ben McRae
I understand what try-catch statements do, but from reading the documentation on php.net, I would not be able to implement one into my own code. I need a real example to help me understand.
我了解 try-catch 语句的作用,但是通过阅读 php.net 上的文档,我无法将其实现到我自己的代码中。我需要一个真实的例子来帮助我理解。
How can I turn this example into a try catch statement, if the upload was not successful?
如果上传不成功,我怎样才能把这个例子变成一个 try catch 语句?
$move = move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/uploads/".$_FILES['file']['name']);
if (!$move) {
die ('File didn't upload');
} else {
//opens the uploaded file for extraction
echo "Upload Complete!";
}
This may not be a good example to work with, but any help would be appreciated.
这可能不是一个很好的例子,但任何帮助将不胜感激。
回答by Tom Haigh
You could do it like this.
你可以这样做。
try {
//throw exception if can't move the file
if (!move_uploaded_file( ... )) {
throw new Exception('Could not move file');
}
//do some more things with the file which may also throw an exception
//...
//ok if got here
echo "Upload Complete!";
} catch (Exception $e) {
die ('File did not upload: ' . $e->getMessage());
}
It is a bit pointless for the above example, but you should get the idea. Note that you can throw the exception(s) from anywhere (e.g. within a function/method that you call from withing the try{}) and they will propagate upwards.
上面的例子有点无意义,但你应该明白这个想法。请注意,您可以从任何地方(例如,在您使用 try{} 调用的函数/方法内)抛出异常,并且它们将向上传播。
回答by Alistair Evans
Well, if you want to use exceptions, you could do something like:
好吧,如果你想使用异常,你可以这样做:
function handleUpload() {
$move = move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/uploads/".$_FILES['file']['name']);
if (!$move) {
throw new Exception('File Didnt Upload');
}
}
try {
handleUpload();
echo "File Uploaded Successfully";
} catch(Exception $ex) {
die($ex->getMessage);
}
I know this may seem like bloat - but you can call the method from anywhere in the call stack, and catch the exception at any point.
我知道这可能看起来很膨胀 - 但是您可以从调用堆栈中的任何位置调用该方法,并在任何时候捕获异常。
回答by Sean
try-catchstatements are used to handle exceptions. I don't believe that the function move_uploaded_filescan throw and exception, as such I think that you code is written is correct. After the call, you look at the return code. If it's false, you end processing, otherwise you are report success.
try-catch语句用于处理异常。我不相信该函数move_uploaded_files可以抛出和异常,因此我认为您编写的代码是正确的。调用后,您查看返回码。如果为false,则结束处理,否则报告成功。
回答by Sean
According to a similar post in PHPbug, only OO code (Object-Oriented code) throws exceptions. That would mean that functions such as move_uploaded_file won't throw their own exceptions, but some other code will.
根据 PHPbug 中的类似帖子,只有 OO 代码(面向对象的代码)会引发异常。这意味着像 move_uploaded_file 这样的函数不会抛出它们自己的异常,但其他一些代码会。
回答by Simon Hymanson
回答by HMagdy
You have to take a look on [Exception Best Practices in PHP 5.3][1]
你必须看看[PHP 5.3中的异常最佳实践][1]
Exception handling in PHP is not a new feature by any stretch. In the following link, you'll see two new features in PHP 5.3 based around exceptions. The first is nested exceptions and the second is a new set of exception types offered by the SPL extension (which is now a core extension of the PHP runtime). Both of these new features have found their way into the book of best best practices and deserve to be examined in detail.
PHP 中的异常处理无论如何都不是一个新特性。在以下链接中,您将看到 PHP 5.3 中基于异常的两个新功能。第一个是嵌套异常,第二个是 SPL 扩展(现在是 PHP 运行时的核心扩展)提供的一组新异常类型。这两个新功能都已收录在最佳实践书中,值得详细研究。
http://ralphschindler.com/2010/09/15/exception-best-practices-in-php-5-3
http://ralphschindler.com/2010/09/15/exception-best-practices-in-php-5-3

