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 征集Python提取PDF文字属性的代码
  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?