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条)

报告相同问题?

悬赏问题

  • ¥30 求一下解题思路,完全不懂。
  • ¥30 关于#硬件工程#的问题:求一下解题思路
  • ¥15 运筹学对偶单纯行法构造扩充问题
  • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 多ip服务器站群如何搭建l2tp服务器
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
  • ¥15 Sharepoint JS开发 付费技术指导