PHP 检查是否设置为两者之一

PHP check if isset one of the two

我正在尝试检查两个输入,但是当只设置两个输入之一时,它应该为 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'];
           

尝试了很多东西,但它并没有像我想要的那样工作。

一种替代方法(更简洁的恕我直言)是预先进行验证,然后在知道您拥有有效数据后进行处理。

开始处理时,您还需要检查需要处理的来源 - $_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
    }
}