Php 嵌套if(isset)未按预期工作

Php 嵌套if(isset)未按预期工作,php,if-statement,Php,If Statement,我有一个嵌套的if(isset),但它似乎没有按照我的预期工作。我还尝试了如果(!empty)。请参阅下面的代码 if (isset($_POST['submit'])){ if(isset($_FILES['userFile'])){ //upload file, make tumbnail, then put info to the the tables on the database //header location } else if (isset($_

我有一个嵌套的
if(isset)
,但它似乎没有按照我的预期工作。我还尝试了
如果(!empty)
。请参阅下面的代码

if (isset($_POST['submit'])){
   if(isset($_FILES['userFile'])){
     //upload file, make tumbnail, then put info to the the tables on the database
     //header location
   } else if (isset($_POST['comment'])){
     //put info to the tables on the database
     //header location

   } else {
     die("you didn't write a comment or upload a file");
   }
}
我的意图是让用户上传一张没有评论的图片,或者让他或她的评论没有上传图片,显然不让用户发送一张空表单。我搜索了一下,但没有找到关于嵌套isset或空的太多信息

我走对了吗?若有,;我怎样才能使这个工作

因为当我在没有设置
userFile
的情况下尝试上面的代码时,它仍然会给出
userFile
部分的错误,该部分如下所示:

if($imagesize2 > $max_size2) {
    die("your file is bigger than max size");
} else if($safe_imagetype2 =='image/jpeg' || $safe_imagetype2 =='image/png' || $safe_imagetype2 == 'image/jpg' || $safe_imagetype2 == 'image/gif') {
    move_uploaded_file($safe_uploadTmp2, "./images/$safe_uploadName2");
} else {
    die("it must be gif, jpg or png");
}

感谢您的帮助。

这就是您要找的

if(isset($_FILES['userFile']) && strlen($_FILES['userFile']['inputNAME']) > 1)

这就是你要找的

if(isset($_FILES['userFile']) && strlen($_FILES['userFile']['inputNAME']) > 1)
您应该这样检查:

if (isset($_FILES['userFile']) && UPLOAD_ERR_OK == $_FILES['userFile']['error']) {
    // The file has been uploaded successfully
}
您应该这样检查:

if (isset($_FILES['userFile']) && UPLOAD_ERR_OK == $_FILES['userFile']['error']) {
    // The file has been uploaded successfully
}

我将从调试这个开始。尝试将您的帖子内容变为var_dump并查看其中的内容。文件上载到$_文件,而不是$_POST。@MarcB my bad原始代码将其作为
$_文件['userFile']
,我也会编辑该问题。您会得到什么错误?检查是否存在
$\u文件['userFile']
不是检查文件是否已上载的方式。如果你有一个
,你会得到一个$\u文件条目,不管上传了什么(如果有的话)。您需要检查
$\u FILES['userFIle']['error']
以查看发生了什么。正如我试图解释的那样,当我试图在不设置文件的情况下发布表单时,我得到了
否则{die(“它必须是gif、jpg或png”);
部分。所以我想是的
if(isset($\u FILES['userFile'])
part被忽略了。我将从调试这个开始。尝试将您的帖子内容变为var_dump并查看其中的内容。文件上载到$_文件,而不是$_POST。@MarcB my bad原始代码将其作为
$_文件['userFile']
,我也会编辑该问题。您会得到什么错误?检查是否存在
$\u文件['userFile']
不是检查文件是否已上载的方式。如果你有一个
,你会得到一个$\u文件条目,不管上传了什么(如果有的话)。您需要检查
$\u FILES['userFIle']['error']
以查看发生了什么。正如我试图解释的那样,当我试图在不设置文件的情况下发布表单时,我得到了
否则{die(“它必须是gif、jpg或png”);
部分。是的,我猜
如果(isset($\u FILES['userFile'])
部分被忽略了。这允许我在没有设置的情况下传递用户文件部分,但现在它显示“你没有输入评论或上传图片”,所以现在我无法检查评论部分。我试着用注释部分
strlen
<代码>}如果(!empty($\u POST['comments'])和&strlen($\u POST['comments'])>1{仍然无法编辑:我给了你1张upvote,因为它在第一部分有效。当我发现我的错误时,我可能会接受你的答案。谢谢你这让我在没有设置的情况下通过用户文件部分,但现在它说“你没有输入评论或上传图片”所以现在我无法检查注释部分。我尝试使用注释部分
strlen
}否则(!empty($\u POST['comments'])和&strlen($\u POST['comments'])>1){
仍不工作编辑:我给了你1票,因为第一部分有效。当我发现我的错误时,我可能会接受你的答案。谢谢你,一点也不。这实际上是确保文件已成功上载的唯一方法。一点也不。这实际上是确保文件已成功上载的唯一方法F非常高兴。