PHP 不会因文件上传大小不正确而引发异常
我有一个带有文件上传输入的简单表单。这是处理该表单的代码:
$error = false;
if(isset($_POST["name"], $_POST["desc"], $_POST["aname"], $_FILES["skin"])) {
try {
// Validate the form
if(strlen($_POST["name"]) < 3 || strlen($_POST["name"]) > 24)
throw new Exception("Skin name needs to be between 3 and 24 characters.");
if(strlen($_POST["desc"]) < 3 || strlen($_POST["desc"]) > 32)
throw new Exception("Skin description must be between 3 and 32 characters.");
if(strlen($_POST["aname"]) < 2 || strlen($_POST["aname"]) > 16)
throw new Exception("Authors name must be between 2 and 16 characters.");
// Validate the file upload
if($_FILES["skin"]["error"] !== UPLOAD_ERR_OK)
throw new Exception("There was a problem uploading the file. Try again later.");
if($_FILES["skin"]["size"] > 204800)
throw new Exception("File size must be 200KB or less.");
} catch (Exception $e) {
$error = $e->getMessage();
}
}
然后在我的代码中我有这样的:
if($error) {
echo "<p class=\"error\">Error: <i>$error</i></p>";
}
如果代码中较早抛出异常,则会在表单上方显示错误。除了本节之外,一切正常:
if($_FILES["skin"]["size"] > 204800)
throw new Exception("File size must be 200KB or less.");
我想做的是检测上传文件的文件大小是否超过 200KB。如果是的话,我想抛出一个异常。但是,如果我通过上传表单发送一个 20MB 的文件,它根本不会捕获它,也不会抛出异常。
谁能猜出为什么吗?我已经尝试了几种不同的组合,但由于某种原因,它只是没有检测到它的大小超过 200KB。
如有任何帮助,我们将不胜感激,谢谢。
编辑:没关系,我忘记更改限制 POST 上传大小的默认 php.ini 设置。对于遇到此问题的任何人,请进入您的 php.ini 文件并将此选项:post_max_size = 8M
更改为适合您需要的内容。
I have a simple form with a file upload input. This is the code that handles that form:
$error = false;
if(isset($_POST["name"], $_POST["desc"], $_POST["aname"], $_FILES["skin"])) {
try {
// Validate the form
if(strlen($_POST["name"]) < 3 || strlen($_POST["name"]) > 24)
throw new Exception("Skin name needs to be between 3 and 24 characters.");
if(strlen($_POST["desc"]) < 3 || strlen($_POST["desc"]) > 32)
throw new Exception("Skin description must be between 3 and 32 characters.");
if(strlen($_POST["aname"]) < 2 || strlen($_POST["aname"]) > 16)
throw new Exception("Authors name must be between 2 and 16 characters.");
// Validate the file upload
if($_FILES["skin"]["error"] !== UPLOAD_ERR_OK)
throw new Exception("There was a problem uploading the file. Try again later.");
if($_FILES["skin"]["size"] > 204800)
throw new Exception("File size must be 200KB or less.");
} catch (Exception $e) {
$error = $e->getMessage();
}
}
And then later down in my code I have this:
if($error) {
echo "<p class=\"error\">Error: <i>$error</i></p>";
}
Which displays an error above the form if an exception was thrown earlier in the code. Everything works fine apart from this section:
if($_FILES["skin"]["size"] > 204800)
throw new Exception("File size must be 200KB or less.");
What I'm trying to do is detect if the file size of the uploaded file is more than 200KB. If it is, I want to throw an exception. But, if I send a 20MB file through the upload form, it doesn't catch it at all, and doesn't throw the exception.
Can anyone have a guess at why? I've already tried a few different combinations but for some reason it just doesn't detect it as over 200KB in size.
Any help is appreciated, thanks.
EDIT: Nevermind, I forgot to change the default php.ini setting that limits POST upload sizes. For anyone that ever has this problem, go into your php.ini file and change this option: post_max_size = 8M
to something that suites your needs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来你的 $_FILES 是空的。如果发布的文件大小超过 php.ini 中的 post_max_size,则可能会发生这种情况。
It seems you get empty $_FILES. This might happens if the posted file size exceeds post_max_size in php.ini.