Codeigniter : The filetype you are attempting to u

2020-05-06 21:24发布

问题:

My codeigniter app suddenly broke today. I didn't work on the upload code and when I tried to upload an image today I suddenly got "The filetype you are attempting to upload is not allowed." Yesterday all was fine.

My config array is:

$config = array(
     'file_name' => $data['slug'] .'-'. $key,
     'upload_path' => './images',
     'allowed_types' => 'gif|jpg|jpeg|png'
    );

I have also tried to set allowed types as : 'image/gif|image/jpg|image/jpeg|image/png' but no luck.

The dumping $this->upload->data() :

Array
(
    [file_name] => ring.jpg
    [file_type] => image/jpeg
    [file_path] => /home/user/www.domain.ca/images/
    [full_path] => /home/user/www.domain.ca/images/ring.jpg
    [raw_name] => ring
    [orig_name] => 
    [client_name] => ring.jpg
    [file_ext] => .jpg
    [file_size] => 49158
    [is_image] => 1
    [image_width] => 
    [image_height] => 
    [image_type] => 
    [image_size_str] => 
)

I understand that there were some bugs in previous versions of CI, but I didn't have these issues yesterday. I also understand that this error only occurs if the MIME type does not match what is allowed, but that does not seem to be the case.

The server is Apache.

Does anyone have any idea what could be the problem? Thanks!

回答1:

I've had this problem twice, here are the two solutions that have worked for me;

1 - The mime type reported by the system can vary, and may not be in the array of mime types set for a particular file extension. .csv files are the worst I have found for this. Find out what mime type the system is reporting by dumping the upload data after your upload

die($this->upload->data());

The 'file_type' index will contain a string - add this to the array value with the index that matches the desired file extension in application/config/mimes.php

2 - The most recent time I have had this is when I upgraded my core Codeiginter version from 2.x to 3.x. I updated the contents of the system directory, but not application/config. I didn't spot the fact that the application/config/mimes.php file is slightly different - the newer version returns an array whereas the old one included it as a var. The fix was to copy in the newer version of mimes.php



回答2:

Have you initialize the $config properly?

$this->load->library('upload', $config);
$this->upload->initialize($config); // Make sure it has been initialized


回答3:

I had the same problem today.

What I did is just to copy the whole content of CodeIgniter-3.0.6\application\config\mimes.php to my application\config\mimes.php.



回答4:

Not sure if this helps, I had this problem only on my Windows Dev Machine running on a test server on local host, on my live web hosting it worked fine detecting mime types.

I had a quick look at the upload library itself, and I found that you can just stick a * in the allowed types to allow anything.

Don't think this was documented anywhere in the manual, but sorted the problem for me as I just wanted to get my script up and running locally to do some work on it.