PHP / CodeIgniter - $_FILES being ignored completely
I have the following in my view
<input class="file" name="mpfile[]" type="file" size="32" />
In my controller, i have the following code.
if(isset($_FILES['mpfile'])) {
echo 'testing';
}
Fairly simple yes? .... E开发者_StackOverflow中文版xcept that every time i run it, no matter if i have choosen a file or not, it runs ... Should it only run the echo if i have a file ready for input?
Make sure your FORM element has the following attributes:
method="POST"
and
enctype="multipart/form-data"
- Christian
check your PHP.ini for:
file_uploads = "1"
post_max_size = 50000
upload_max_file_size = 2M
do a phpinfo();
disclaimer php.ini values might be slightly off, but you'll find them I'm sure. the names are right, not sure if the values are.
Assuming the <input>
is embedded in a <form>
that does a POST
on your script, have you tried dumping $_FILES
? E.g.:
if ('POST' == $_SERVER['REQUEST_METHOD'])
{
print '<pre>'.var_export($_FILES,TRUE).'</pre>';
}
Can you post a result? Also, are uploads enabled for PHP?
Have you tried using Codeigniter's upload class. Codeigniter could be emptying $_FILES
along with the other globals.
Maybe try dumping $_FILES
at the top of codeigniters index.php to make sure
if(isset($_FILES['mpfile']['name']) && $_FILES['mpfile']['name'])
{
echo 'testing';
}
Here's another stab in the dark. Check the error code:
if($_FILES['mpfile']['error'] != UPLOAD_ERR_NO_FILE) {
echo 'testing';
}
You also need to check for an empty file:
if (isset($_FILES['userfile']) && $_FILES['userfile']['error'] != 4)
See this post:
http://codeigniter.com/forums/viewthread/136610/#673652
"The upload field gets put into $_FILES even if it is empty and the error and size are set to 4 and 0 respectively"
Edit to add:
I've never uploaded multiple, but I think in your case you would add ['mpfile']
before each ['userfile']
in the test
if (!empty($_FILES['mpfile']['name'])) {
var_dump($_FILES['mpfile']['name']);
}
You are defining an array:
<input class="file" name="mpfile[]" type="file" size="32" />
In PHP the variable $_FILES['mpfile']
will be also an (empty) array.
If you now test with isset
then it is set because it's an array (and never null).
Test with empty()
.
精彩评论