I’m having problems uploading files with CodeIgniter 2.1.0, as I recieve the $_FILES array empty.
This is the form:
<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>
The action in the rendered form takes the value: http://localhost/nicUpload/test.
This is the controller:
<?php
class NicUpload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function test() {
echo count($_FILES);
}
}
?>
The result is 0, I would expect 1.
I tried doing the same without 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);
?>
and I get the expected result (1). So it’s not a php configuration problem.
** UPDATE **
I should have said it earlier, but if I use CodeIgniter’s Upload class it fails in this lines of CI’s 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;
}
as $_FILES is empty.
Ok, thanks to Sena I found the problem. I was using the method described here to use i18n, so when I was uploading to
http://localhost/nicUpload/testI was being redirected tohttp://localhost/spa/nicUpload/test, in that redirections the information in$_FILESwas getting lost. So I just had to addnicUploadto the$specialarray inMY_Lang.php:That fixed it, once
$_FILEShad the proper information I could use the proper method to upload files (the method that Sena mentioned).