我在使用 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
是空的。