Welcome Guest, Not a member yet? Register   Sign In
Problem Uploading Mp3 and Video files
#11

[eluser]~Chris~[/eluser]
I am having this problem as well. It is very wierd. bugzbrown, I tried your solution, but to no avail.
in my controller I have:
Code:
...
$config['upload_path'] = SERVER_PATH.'/assets/uploads/tmp';
$config['allowed_types'] = 'gif|jpg|jpeg|jpe|png|mp3';
$config['max_size']    = '2048';
            
$this->load->library('upload', $config);

And mime types:
Code:
...
'mp3'    =>    array('audio/mpeg','audio/mpeg3','audio/mpg','audio/x-mpeg','audio/mp3','application/force-download','application/octet-stream'),
'jpg'    =>    array('image/jpeg', 'image/pjpeg', 'application/octet-stream'),
...

The only difference is I am using swfuploader. Never had this problem before (I've used swfupload in many projects). Using swfupload, I know all mimes need to have the application/octet-stream to work

anyway.
All images upload fine no problem.
When I upload an MP3 ( < 2mb of course because of the size limit) i get the error:
Code:
The filetype you are attempting to upload is not allowed.
So, I am scratching me head on this one.

Edit:
Also just tried this combo:
Code:
'mp3'   =>      array('audio/mpeg', 'audio/x-mpeg', 'audio/mp3','audio/x-mp3','audio/mpeg3','audio/x-mpeg3','audio/mpg','audio/x-mpg','audio/x-mpegaudio', 'application/octet-stream'),
Still did not work.

Another Edit:
I just checked the Mime-Type of the incoming file, and it is
application/octet-stream so now I am just stumped why code igniter says it is not allowed. (the extension is .mp3 of the file I'm uploading btw)

Here is all the info under the $_FILES['userfile']:
Code:
$_FILES['userfile'] = {"name":"test.mp3","type":"application/octet-stream","tmp_name":"/tmp/phpkqLbnQ","error":0,"size":2012708}

Also, I did this from a regular HTML upload form (without swfuploader) got the same thing. File type not allowed. Sad
#12

[eluser]~Chris~[/eluser]
Ok, well I figured it out. Hopefully this helps someone still...

In my config array, I had it like this:
Code:
..
$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:
Code:
..
$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)
Code:
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
Code:
$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.
#13

[eluser]vitoco[/eluser]
i had the same problem...didn't recognize the filetype and give me this message

Code:
The filetype you are attempting to upload is not allowed.

well, the problem wasn't the mime types in mimes.php, it was that the server ( i guess ) added '\"' or '"' to the ['type'] var, sonow i use this to clean the extra quotes.

Code:
$_FILES['user_file']['type'] = str_replace('\"' , '' , $_FILES['user_file']['type'] );

$_FILES['user_file']['type'] = str_replace('"' , '' , $_FILES['user_file']['type'] );

i know it's not elegant....but it works
#14

[eluser]Sander Versluys[/eluser]
@~Chris~

Awesome man! This really droved me crazy! Thanks!
#15

[eluser]BoogieK[/eluser]
Hy,

I know it`s kind of late to post a reply/solution for this problem but here is what I`ve found.

The problem is from the Upload.php Class in is_allowed_filetype() function because there is a line that says:
Code:
in_array($val, $image_types))
than return false.

The problem is that $val is not our file extension so what I`ve done was some changes in this function. In fact, I`ve changed this function to this:
Code:
function is_allowed_filetype()
    {
        if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types))
        {
            $this->set_error('upload_no_file_types');
            return FALSE;
        }
                
        $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');
                
                if(!in_array(strtolower(preg_replace('/\./', '', $this->file_ext)), $this->allowed_types)){
                    return FALSE;
                }

        foreach ($this->allowed_types as $val)
        {
            $mime = $this->mimes_types(strtolower($val));
                        
                        if(is_array($mime) && (in_array($this->file_type, $mime))){
                            return TRUE;
                        }elseif((is_string($mime)) && ($mime === $this->file_type)) {
                            return TRUE;
                        }
                        
                        if((in_array(strtolower(preg_replace('/\./', '', $this->file_ext)), $image_types)) && (getimagesize($this->file_temp)) == false){
                            return FALSE;
                        }
        }
        
        return FALSE;
    }

Now, if someone have some problems with this function, or if something is not correct, than please tell me. For me, this function works great.

In fact, for .mp3 file format which has the audio/mp3 file type I use the mime type
Code:
'mp3'    =>    array('audio/mpeg', 'audio/mpeg3', 'audio/mpg', 'audio/x-mpeg', 'audio/mp3'),
So please feel free to use this function till something goes wrong. Smile
#16

[eluser]mahrizal[/eluser]
[quote author="vitoco" date="1248838061"]i had the same problem...didn't recognize the filetype and give me this message

Code:
The filetype you are attempting to upload is not allowed.

well, the problem wasn't the mime types in mimes.php, it was that the server ( i guess ) added '\"' or '"' to the ['type'] var, sonow i use this to clean the extra quotes.

Code:
$_FILES['user_file']['type'] = str_replace('\"' , '' , $_FILES['user_file']['type'] );

$_FILES['user_file']['type'] = str_replace('"' , '' , $_FILES['user_file']['type'] );

i know it's not elegant....but it works[/quote]

@vitoco

thank's a lot

it works for me
#17

[eluser]matt6805[/eluser]
[quote author="Zeeshan Rasool" date="1214478658"]Guys ,i think this one can make it posible plz try it ,

'mp3' => array('audio/mpeg','audio/mpeg3','audio/mpg','audio/x-mpeg','audio/mp3'),
Hope it works.
.................JaMzEe[/quote]

Just wanted to say Zeeshan's fix works for me at least in Firefox. Haven't tested other browsers yet. Thanks Zeeshan!
#18

[eluser]Charles Garrison[/eluser]
[quote author="Zeeshan Rasool" date="1214478658"]Guys ,i think this one can make it posible plz try it ,

'mp3' => array('audio/mpeg','audio/mpeg3','audio/mpg','audio/x-mpeg','audio/mp3'),
Hope it works.
.................JaMzEe[/quote]

This fixed my problem. I modified the quotes however, because the ones used in this code snippet are fancy-quotes.
#19

[eluser]Unknown[/eluser]
Hi guys,

I was uploading an image and an audio file. The reason I was getting this error was, I would initialize the library with the extension jpg,png, etc. When I would try to upload the audio file, I would try to load the already loaded 'upload' library by

Code:
$this->load->library('upload', $config);

So, after calling the load for the png, jpg file, I would call the
Code:
$this->upload->set_allowed_types('mp3|mp2');
$this->upload->set_upload_path( $path );

Hope this helps someone.
#20

[eluser]inoy[/eluser]
I've tried by the same way but have not succeeded, his solution how??




Theme © iAndrew 2016 - Forum software by © MyBB