我正在尝试检查两个输入,但是当仅设置两个输入之一时,它应该给出 TRUE。当两者都为空时,它应该给出错误。 //check if post image upload or youtube url isset $pyturl = $_POST['post_yturl']; if (isset($_FILES['post_image']) && isset($pyturl)) { if (empty($_FILES['post_image']['name']) or empty($pyturl)) { $errors = '<div class="error2">Choose a news header.</div>'; } else { //check image format $allowed = array('jpg','jpeg','gif','png'); $file_name = $_FILES['post_image']['name']; $file_extn = strtolower(end(explode('.', $file_name))); $file_temp = $_FILES['post_image']['tmp_name']; 尝试了多种方法,但它不想按我想要的方式工作。
1 回答
一只甜甜圈
TA贡献1836条经验 获得超5个赞
一种替代方案(恕我直言,更干净)是预先进行验证,然后在知道拥有有效数据后对其进行处理。
当您开始处理时,您还需要检查需要处理的来源 -$_FILE与。$_POST
<?php
$isValid = true;
// Validation - if both sources are empty, validation should fail.
if (!isset($_FILES['post_image']) && !isset($_POST['post_yturl'])) {
$isValid = false;
$errors = '<div class="error2">Choose a news header.</div>';
}
... More validation if needed
if ($isValid) {
// At this point you know you have at least 1 source. Start processing.
if (isset($_FILES['post_image']) {
... do your processing
}
if (isset($_POST['post_yturl']) {
... do your processing
}
}
- 1 回答
- 0 关注
- 80 浏览
添加回答
举报
0/150
提交
取消