8

I'm trying to upload a css file user codigniter's upload class. Here is the controller:

public function uploadcssfile() 
{
   $config['upload_path'] = './stylesheets/stores/'.$memberid.'/'; 
   $config['file_name'] = 'main.css';    
   $config['allowed_types'] = 'css';   
   $config['overwrite'] = true;   
   $this->load->library('upload', $config);
   if (! $this->upload->do_upload('filefieldname'))
   {
     die($this->upload->display_errors());
   } 
}

I just want to upload a file of type css, yet codeigniter always gives this error:

The filetype you are attempting to upload is not allowed.
user1664427
  • 237
  • 4
  • 12
  • I'm just guessing, try `$config['allowed_types'] = 'css|text/css';` – dbf Sep 12 '12 at 02:01
  • sorry had a type, `test/css` should have been `text/css` obviously ;) – dbf Sep 12 '12 at 02:06
  • Just a sanity check; it's not just that the file you are uploading *is* css, it also *has the .css extension*, right? – Chris Trahey Sep 12 '12 at 02:12
  • `$this->upload->do_upload('filefieldname')` I don't think it expects any parameters, so just `$this->upload->do_upload();` Did you initialise the helper method? e.g. `$this->load->helper(array('form', 'url'));` – dbf Sep 12 '12 at 02:14
  • @ctrahey yes it has the .css extention. – user1664427 Sep 12 '12 at 02:23
  • 2
    Are you using the latest version of ci 2.1.2? There were some big fixes with MIME types – Laurence Sep 12 '12 at 02:28
  • how do i check which version i have? – user1664427 Sep 12 '12 at 02:29
  • just remove this line `$config['allowed_types'] = 'css';` and then try it – chhameed Sep 12 '12 at 04:51
  • Note: By default the upload routine expects the file to come from a form field called `userfile`, and the form must be a `multipart` type: `
    `
    – Jordan Arsenault Sep 12 '12 at 06:40
  • @TheShiftExchange has a point. To find out your version of CI, go to `system/core/CodeIgniter.php`. Around line 36 you'll find the line `define('CI_VERSION', '2.x.x');` – Mudshark Sep 12 '12 at 07:15
  • Are you able to successfully upload some other type of file? I'm not certain that your issue is only with css files. – Catfish Sep 12 '12 at 19:40

1 Answers1

3

I know, this is really weird but it worked for me:

in application/config/mimes.php, line 77

'css' => 'text/css',

change to:

'css' => array('text/css','text/x-c'),

To see, if your development environment has its own sense of humour and can add new mime types to any text files (my case), check your mimes like this:

if ( ! $this->upload->do_upload(-your-form-input-name-) ){
                    print_r($_FILES['-your-form-input-name-']['type']);
                    exit($this->upload->display_errors(). 'File Type: ' . $this->upload->file_type);
                }

If an upload fails it will show you what mime type your server is actually getting to deal with.

PS. Now that there is an answer to your question, go here and help me :)

Community
  • 1
  • 1
pop
  • 3,464
  • 3
  • 26
  • 43