dpj997991 2018-05-02 20:22
浏览 70
已采纳

对于html表单输入,PHP总是将$ _POST数组isset /!empty / sizeof> 0评估为true

I have a basic form to upload an image:

<?php
  if(!empty($_POST['?upload_img'])) {
      echo "true<br>";
  } else { echo "false<br>"; }
?>

<html>
  <form action='' method='post' enctype='multipart/form-data'>
    <input type='file' name='input_img' id='input_img'><br>
    <input type='submit' value='Upload Image' name='?upload_img'>
  </form>
</html>

I would like a PHP boolean/evaluation pair to run only once for each upload.

I have tried evaluating using !empty,isset,sizeof(foo)>0

I have also tried to find a solution using $_SESSIONs

Is there are correct use of unset or $_POST=array()/$_FILE=array() that I have missed?

I can achieve the desired behaviour with sessions:

<?php
  session_start();
  if(!empty($_SESSION['foo'])) {
    echo "true<br>";
  } else { echo "false<br>"; }
?>

<html>
  <?php
    if(!sizeof($_SESSION['foo'])) {
      $_SESSION['foo']=array();
      $_SESSION['foo']['bar']="path/to/the/file.png";
    } else { $_SESSION=array(); }
  ?>
</html>

How can I achieve the same with input from a <form> instead?

Any help is much appreciated!

  • 写回答

2条回答 默认 最新

  • duanjiaonie6097 2018-05-05 00:39
    关注

    For completeness, an outline approach specific to my issue:

    <!--filename:foo.php-->
    <?php
      session_start();
      if(isset($_POST['?confirm'])) {
        $_SESSION['input']=$_FILES['input'];
        header("HTTP/1.1 303");
        header("Location: http://$_SERVER[HTTP_HOST]/foo.php");
        die;
      } else if(!empty($_SESSION['input'])) {
        echo "true<br>";
        session_unset();
        session_destroy();
      } else { echo "false<br>"; }
    ?>
    <html>
      <form action='foo.php' method='post' enctype='multipart/form-data'>
        <input type='file' name='input' id='input'><br>
        <input type='submit' value='confirm' name='?confirm'>
      </form>
    </html>
    

    Based on Post-Redirect-Get Pattern in PHP

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上
  • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法
  • ¥15 dnat基础问题,本机发出,别人返回的包,不能命中
  • ¥15 请各位帮我看看是哪里出了问题