EllisLab text mark
Advanced Search
1 of 3
1
   
File Upload Class - MIME type detection error - 2.1.0
Posted: 17 November 2011 03:35 PM
Joined: 2010-07-22
5 posts

There is a bug with the File Upload Class in the _file_mime_type function.

1. Uploading any image with the following config would generate the error ‘The filetype you are attempting to upload is not allowed.’:

$config = array(
 
'upload_path' => './uploads/',
 
'allowed_types' => 'gif|jpg|png'
);
  
$this->load->library('upload'$config); 


2. Changing ‘allowed_types’ to ‘*’ allows the file to be uploaded, however the upload data array ( $this->upload->data() ) contains an error:

[file_type] => cannot open `' (no such file or directory) 

3. Looking at system/libraries/Upload.php , Line 1058 tries to use an array value that does not exist.

@exec('file --brief --mime-type ' escapeshellarg($file['tmp_path'])$output$return_code)

Changed to:

@exec('file --brief --mime-type ' escapeshellarg($file['tmp_name'])$output$return_code)

This seems to detect the proper MIME type.

——

System Info: PHP 5.2.11 (MAMP 1.0) - Mac OS X 10.6.8

 

 
Posted: 18 November 2011 05:41 AM   [ # 1 ]   [ Rating: 0 ]
Joined: 2007-05-25
15 posts

hi, i didn’t want to start a new thread for this cos i am having a simliar issue.

I updated my site to 2.1.0 yesterday everything else works fine apart from the file upoloading.

$config['upload_path''./resources/documents/uploads/';
$config['allowed_types''pdf|doc|docx';
$config['encrypt_name'true;
 
$this->load->library('upload'$config);
 
if(!
$this->upload->do_upload($field)):
    
 return array(
'error' => $this->upload->display_errors());
   
else:
...
endif; 

I am have tried uploading all of the allowed type but i just get an error message stating that the file uploaded is not allowed because its the wrong type.

If i change the file upload library back to the 2.0.3 version i was using it all works fine.

Maybe a bug in hte mime detection?

 

 
Posted: 18 November 2011 10:40 AM   [ # 2 ]   [ Rating: 0 ]
Joined: 2011-09-14
3 posts

Everything seems OK with that fix until I try uploading bmp images which are not images anymore apparently.

array(14) {
["file_type"]=> string(14) "image/x-ms-bmp"
["file_ext"]=> string(4) ".bmp"
["is_image"]=> bool(false)
["image_width"]=> string(0) ""
["image_height"]=> string(0) ""
["image_type"]=> string(0) ""
["image_size_str"]=> string(0) ""
}

 
Posted: 18 November 2011 12:06 PM   [ # 3 ]   [ Rating: 0 ]
Joined: 2010-07-22
5 posts
Karagath - 18 November 2011 10:40 AM

Everything seems OK with that fix until I try uploading bmp images which are not images anymore apparently.

array(14) {
["file_type"]=> string(14) "image/x-ms-bmp"
["file_ext"]=> string(4) ".bmp"
["is_image"]=> bool(false)
["image_width"]=> string(0) ""
["image_height"]=> string(0) ""
["image_type"]=> string(0) ""
["image_size_str"]=> string(0) ""
}

You can probably update your config/mimes.php to reflect that MIME type; or just revert to an old/working version of system/libraries/Upload.php that doesn’t include this new MIME type detection.

 
Posted: 21 November 2011 10:14 AM   [ # 4 ]   [ Rating: 0 ]
Joined: 2011-09-14
3 posts
brohde - 18 November 2011 12:06 PM

You can probably update your config/mimes.php to reflect that MIME type; or just revert to an old/working version of system/libraries/Upload.php that doesn’t include this new MIME type detection.

Ah silly me, it did work to update the mime but bmp is still not registered as an image. Thanks for the knock on the noggin though smile

 
Posted: 21 November 2011 11:16 PM   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2009-01-14
8 posts

thanks,
it works well for me.

brohde - 17 November 2011 03:35 PM

There is a bug with the File Upload Class in the _file_mime_type function.

1. Uploading any image with the following config would generate the error ‘The filetype you are attempting to upload is not allowed.’:

$config = array(
 
'upload_path' => './uploads/',
 
'allowed_types' => 'gif|jpg|png'
);
  
$this->load->library('upload'$config); 


2. Changing ‘allowed_types’ to ‘*’ allows the file to be uploaded, however the upload data array ( $this->upload->data() ) contains an error:

[file_type] => cannot open `' (no such file or directory) 

3. Looking at system/libraries/Upload.php , Line 1058 tries to use an array value that does not exist.

@exec('file --brief --mime-type ' escapeshellarg($file['tmp_path'])$output$return_code)

Changed to:

@exec('file --brief --mime-type ' escapeshellarg($file['tmp_name'])$output$return_code)

This seems to detect the proper MIME type.

——

System Info: PHP 5.2.11 (MAMP 1.0) - Mac OS X 10.6.8

 
Posted: 01 December 2011 10:22 AM   [ # 6 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-10
183 posts

Even with this fix, I can not upload anything… I have 2 places in my app with uploading. One for images, one for text documents. Both are giving an error “file type not allowed” :(

Any other ideas?

 
Posted: 01 December 2011 06:22 PM   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2009-07-08
13 posts

Yah this bug was driving me nuts. Doing the following totally worked for me:

Looking at system/libraries/Upload.php , Line 1058 tries to use an array value that does not exist.

@exec('file --brief --mime-type ' escapeshellarg($file['tmp_path'])$output$return_code); 

Changed to:

@exec('file --brief --mime-type ' escapeshellarg($file['tmp_name'])$output$return_code); 

I was using Plupload with Flash or HTML5 uploading. This issue has been officially patched as of the 2.1 Stable branch

 
Posted: 01 December 2011 06:51 PM   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-10
183 posts

Nope… I’m still getting this error. I have

'allowed_types' => 'gif|jpg|png' 

and when i try to upload any image… “the file type is not allowed” error.

:(

 
Posted: 01 December 2011 07:27 PM   [ # 9 ]   [ Rating: 0 ]
Avatar
Joined: 2009-07-08
13 posts

try

'allowed_types' => 'gif|jpg|jpe|png' 
 
Posted: 02 December 2011 02:59 AM   [ # 10 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-10
183 posts

These are my 2 pieces of code, where I’m using the upload:

$prefs = array(
    
'allowed_types' => 'odt|doc|docx',
    
'upload_path' => './uploads/',
    
'max_size' => 2048
   
);

   
$this->load->library('upload'$prefs); 

and:

$prefs = array(
   
'allowed_types' => 'gif|jpg|jpeg|png',
   
'upload_path' => './uploads/',
   
'max_width' => '1000',
   
'max_height' => '1000',
   
'max_size' => 2048,
   
'overwrite' => TRUE
  
);

  
$this->load->library('upload'$prefs); 

The first one is for word documents, the second one for images (avatar uploading). The code worked perfectly until the update to 2.1.0
And trust me, the problem is not in the files. I’ve tried with .jpg, .gif, .png (for the images) and with .doc, .docx for the documents.
I applied the suggested fix and still no change :(

EDIT: I can send my /system/libraries/Upload.php to someone, who wants to try with my version of the file (as I said, it is 2.1.0 with this change applied). And if it works for you, then I will say that the problem is in my code. But… This uploading thing worked great before the update to 2.1.0

 
Posted: 02 December 2011 03:05 AM   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2009-07-08
13 posts

Are you doing normal form submit posts or are you using a fancy ajax uploading script? Did you update the config/mimes.php file?

 
Posted: 02 December 2011 03:23 AM   [ # 12 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-10
183 posts
Brennan Novak - 02 December 2011 03:05 AM

Are you doing normal form submit posts or are you using a fancy ajax uploading script? Did you update the config/mimes.php file?

Normal upload, no ajax… And yes, updated mimes.php
but… no success :(

Update: I am testing this locally (which means Windows + XAMPP) and not on my hosting (which is linux). If it is important… But I don’t want to upload it with this major problem.

 
Posted: 02 December 2011 03:29 AM   [ # 13 ]   [ Rating: 0 ]
Avatar
Joined: 2009-07-08
13 posts

Shoot man. I really don’t know what to suggest! The above fixed thankfully worked for me (I was tearing my hair out). Yah send me your Upload.php I’ll swap it into my project and see if it works. Also, are you on a *nix stack or a Windows?

 
Posted: 02 December 2011 03:41 AM   [ # 14 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-10
183 posts

Thanks, Brennan… But even the forum has some problems with the upload smile
I’ve tried to send you a PM with the file attached, but…

Error
The file could not be written to disk.

Will try with an email now smile

 
Posted: 02 December 2011 07:30 AM   [ # 15 ]   [ Rating: 0 ]
Avatar
Joined: 2010-07-10
183 posts

Anyway… The guy, who made some changes in the upload class, gave me a fix for my problem. It appears on Windows machines, when sometimes mime_content_type returns false. And the fix is like this… On line 1058 (/system/libraries/Upload.php):

$this->file_type = @mime_content_type($file['tmp_name']);
return; 

Replace with:

$this->file_type = @mime_content_type($file['tmp_name']);
if (
strlen($this->file_type) > 0) return; 

I can not say if this fix is a good solution, but anyway it works great for me. So maybe some of the developers can give us a hint here.

Edit: This is the fix, with the pull request on GitHub from my friend:
https://github.com/EllisLab/CodeIgniter/pull/733

 
1 of 3
1