我在使用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
为空。
答案 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提到的方法)。