我试图在表单发布时添加if else语句,而不是用户是否包含文件?

问题描述 投票:1回答:4

我怎么做这样的if / else语句?用户可以选择是否添加图像。如果添加图像,我想要运行if,如果他们不希望别人运行,因为根据是否添加图像,他们将有两个不同的结束。

if (isset($_POST['formsubmitted'])) { 

if (isset($_POST['name'])){  }else{  }

if (isset($_POST['state'])){  }else{  }

if (isset($_FILES['images']['name'])) { echo 'images'; exit;} else {echo 'no images'; exit;}


} #end main form submitted

如果(isset($ _ FILES ['images'] ['name']))不起作用,因为截至目前即使没有提交图像,它仍然会提交图像。

html文件字段是:

<form>
<input type='file' name='images[]' id=''>
<input type='file' name='images[]' id=''>
<input type='file' name='images[]' id=''>
<input type = "submit">    
</form>
php html if-statement isset
4个回答
5
投票

你应该试试这个

Submit.php

<pre>
<?php 
// those index are empty the array filter remove this 

echo "without filter"."<br>";
print_r($_FILES['images']['name']);

echo "filter"."<br>";
$usersFileUpload = array_filter($_FILES['images']['name']);
print_r($usersFileUpload);

$usersFileUploadCount = count($usersFileUpload);

for($i=0;$i<=$usersFileUploadCount;$i++){
    echo $usersFileUpload[$i]."<br>";

    // insert Table     
}
?>

HtmlForm.php

<form action="submit.php" method="post" enctype="multipart/form-data" name="images">
<input type='file' name='images[]' id=''><br />
<input type='file' name='images[]' id=''><br />
<input type='file' name='images[]' id=''>
<input name="" type="submit" />
</form>

查看图片以进行验证1

查看图片以进行验证2


0
投票

我不确定我理解,我想你正在努力做到这一点。

if (isset($_FILES['name']) && isset($_FILES['images']))

0
投票

尝试检查$ _FILES ['images'] ['error'] [$ x]是否等于4.如果我没记错,这表示上传时出错。

显然,对于图像上传器的每次迭代都要这样做,所以基本上用上传器阵列的键替换$ x。

请记住,空数组仍然是一个set数组。

见:http://uk3.php.net/manual/en/features.file-upload.errors.php

另外:http://uk3.php.net/manual/en/features.file-upload.php


0
投票

试试这个

if($_FILES['your-name-file']['error'] == 4) {
            //process your error
    }

这对我有用!

© www.soinside.com 2019 - 2024. All rights reserved.