|
发表于 2012-7-30 21:16:26
|
显示全部楼层
In my config array, I had it like this:
..
$config['allowed_types'] = 'gif|jpg|jpeg|jpe|png|mp3';
$this->load->library('upload', $config);
What you want to do is, put the mp3 in the front, before all the other file types, like so:
..
$config['allowed_types'] = 'mp3|gif|jpg|jpeg|jpe|png';
$this->load->library('upload', $config);
Heres Why:
Here is the part of the function in the Upload.php Core Library that checks for allowed filtypes. I only pasted the important part (the first part)
function is_allowed_filetype()
{
$image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
foreach ($this->allowed_types as $val)
{
$mime = $this->mimes_types(strtolower($val));
// Images get some additional checks
if (in_array($val, $image_types))
{
if (getimagesize($this->file_temp) === FALSE)
{
return FALSE;
}
}
...
Heres what happens. If you have your allowed type with the image file extension first
like
$config['allowed_types'] = 'gif|jpg|jpeg|jpe|png|mp3';
The for loop will check them in order. It will start with gif, then jpg, then jpeg, etc.
The first IF statement in the loop, checks to see if the allowed filetype is an image filetype (as there is a variable array containing image types above it $image_types)
well gif (the first in my allowed types) matches, so into the if statement it goes.
The next if statement, it checks for a valid image, by calling the function getimagesize(). Well, I uploaded an MP3, which is not an image, so getimagesize returns false… into the if statement.
return false. returning false ends the forloop right on the spot returning saying its an invalid filetype, and so it doesn’t continue on the rest of your allowed mime types to see of your MP3 is allowed or not.
if you upload an mp3, and an image format is listed first an your allowed_types config array, it will return Filetype not allowed as it will fail the test of the image types.
Now, if mp3 is listed first, and you upload an image, your ok, because the the first If statement will be skipped while checking the mp3 filetype, as the loop comes around again, it will check the image, getimagesize() wont return false, etc.
So it seems the best solution, is to always list image filetype LAST in your allowed_types config array.
Hope this helps. |
|