【问题标题】:Why isnt $_FILE set为什么没有设置 $FILE
【发布时间】:2015-01-04 05:13:31
【问题描述】:

这是我的 html 表单的 sn-p

<form class="form-horizontal" role="form" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="profilepictureinput">Profile Picture</label>
        <input type="file"
               id="profilepictureinput"
               name="profilepicture">
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

当我发布表单时 php 说

isset($_FILE['profilepicture']) 

返回假。这是为什么呢?

【问题讨论】:

  • 因为是$_FILES
  • 对不起,我使用了 $_FILES,但它仍然无法正常工作
  • 如果是这样,多贴一些代码可能会有所帮助
  • 作为参考,对完整的 $_FILES 数组执行 print_r()。检查其中的实际内容。

标签: php html forms file file-upload


【解决方案1】:

$_FILE 更改为$_FILES

isset($_FILES['profilepicture']) 

如果未提交表单,它将返回false。提交后会返回true

【讨论】:

    【解决方案2】:

    正如 Tom Kriek 已经说过的,正确的语法是:

    $_FILES['profilepicture']
    

    【讨论】:

      【解决方案3】:

      首先,您需要检查 $_POST,如果表单已提交,您才能从 $_FILES 获取信息。 你应该这样写:

      <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data">
          <div class="form-group">
              <label for="profilepictureinput">Profile Picture</label>
              <input type="file" id="profilepictureinput" name="profilepicture">
          </div>
          <button type="submit" class="btn btn-default" name="new-picture">Submit</button>
      </form>
      

      这是变化:

      <button type="submit" class="btn btn-default" name="new-picture">Submit</button>
      

      并与isset($_POST['new-picture']) 核对 upload() 函数打印出 $_FILES 数组的内容,这样就可以看出问题所在了。

      <?php
      //$newImageSubmitted is TRUE if form was submitted, otherwise FALSE
      $newImageSubmitted = isset( $_POST['new-image'] );
      
      if ( $newImageSubmitted ) {
          //this code runs if form was submitted
          $output = upload();
      } else {
          //this runs if form was NOT submitted
          $output = include_once "views/upload-form.php";
      }
      return $output;
      
      function upload(){
          $out = "<pre>";
          $out .=print_r($_FILES, true);
          $out .= "</pre>";
          return $out;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-23
        • 1970-01-01
        • 2013-05-27
        • 2021-03-05
        • 2016-04-06
        • 1970-01-01
        • 1970-01-01
        • 2017-05-12
        相关资源
        最近更新 更多