0

我在使用 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 的情况下做同样的事情:

索引.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是空的。

4

1 回答 1

0

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

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

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

于 2012-06-18T19:41:24.550 回答