无法使用CodeIgniter 2.1.0上传文件

时间:2012-06-18 17:36:35

标签: php codeigniter file-upload codeigniter-2

我在使用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>

upload.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为空。

1 个答案:

答案 0 :(得分:0)

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

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

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