2012-06-18 116 views
0

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

这是以下形式:

<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

我尝试没有笨做同样的:

的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的文件上传类它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

好的,谢谢塞纳我发现了这个问题。我使用的方法描述here使用国际化,所以当我上传到http://localhost/nicUpload/test我被重定向到http://localhost/spa/nicUpload/test,在重定向$_FILES信息迷路了。所以我就不得不添加nicUpload$special数组中MY_Lang.php

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

问题解决了,一旦$_FILES了,我可以用适当的方法来上传文件的正确信息(即塞纳提到的方法)。