【问题标题】:Can't upload files with CodeIgniter 2.1.0无法使用 CodeIgniter 2.1.0 上传文件
【发布时间】:2012-06-18 17:36:35
【问题描述】:

我在使用 CodeIgniter 2.1.0 上传文件时遇到问题,因为我收到的 $_FILES 数组是空的。

这是表格:

<form enctype="multipart/form-data" action="<?= base_url()?>nicUpload/test" method="POST">
  Send this file: <input name="userfile" type="file" />
  <input type="submit" value="Send File" />
</form>

呈现形式中的动作取值:http://localhost/nicUpload/test

这是控制器:

<?php
  class NicUpload extends CI_Controller {

    public function __construct() {
      parent::__construct();
      $this->load->helper(array('form', 'url'));
    }
    function test() {
      echo count($_FILES);
    }
  }
?>

结果是0,我希望是1

我尝试在没有 CodeIgniter 的情况下做同样的事情:

index.php:

<!doctype html>
<html>
  <head></head>
  <body>
    <form enctype="multipart/form-data" action="upload.php" method="POST">
      Send this file: <input name="userfile" type="file" />
      <input type="submit" value="Send File" />
    </form>
  </body>
</html>

上传.php:

<?php
  echo count($_FILES);
?>

我得到了预期的结果 (1)。所以不是php配置问题。

** 更新 **

我应该早点说,但如果我使用 CodeIgniter 的 Upload 类,它会在 CI 的 system/libraries/Upload.php 的这行中失败:

// Is $_FILES[$field] set? If not, no reason to continue.
if ( ! isset($_FILES[$field]))
{
  $this->set_error('upload_no_file_selected');
  return FALSE;
}

因为$_FILES 是空的。

【问题讨论】:

    标签: php codeigniter file-upload codeigniter-2


    【解决方案1】:

    好的,感谢 Sena,我找到了问题所在。我使用here 描述的方法来使用i18n,所以当我上传到http://localhost/nicUpload/test 时,我被重定向到http://localhost/spa/nicUpload/test,在重定向中$_FILES 中的信息丢失了。所以我只需要在MY_Lang.php$special 数组中添加nicUpload

    private $special = array (
      "admin",
      "auth",
      "nicUpload"
    );
    

    解决了这个问题,一旦$_FILES 获得了正确的信息,我就可以使用正确的方法来上传文件(Sena 提到的方法)。

    【讨论】:

      猜你喜欢
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-05
      • 2019-03-01
      • 2017-01-26
      • 2013-10-04
      • 1970-01-01
      相关资源
      最近更新 更多