Rectangle 27 1

php Uploading in Codeigniter The filetype you are attempting to upload is not allowed?


PS: I do this with the Logging class also for when I want to generate custom logs.

This allows me to use the standard CI syntax, and avoid hacking the original files! My library is automatically used because it simply "extends" the original, it's a completely painless solution and won't break if for some reason you have to replace the original files.

What I did was create my own Library "MY_Upload" to extend the CI_Upload Class, then I just copied the CI_Upload class and applied the changes outlined by Adam (thanks a bunch BTW for the solution) in my custom library.

Note
Rectangle 27 1

php Uploading in Codeigniter The filetype you are attempting to upload is not allowed?


I had the same issue. You may need to check if the application recognizes the mimetype of the file that is being uploaded. Adding a new mimetype to config/mimes.php fixed the issue. :-)

Note
Rectangle 27 1

php Uploading in Codeigniter The filetype you are attempting to upload is not allowed?


$config['upload_path'] = './uploads/invoices/';
$config['allowed_types'] = '*'; // add the asterisk instead of extensions
$config['max_size'] = '3000';
$this->load->library('upload', $config);

if (!$this->upload->do_upload('proof_of_purchase'))
{
    $data['error'] = $this->upload->display_errors();
    $data['include'] = 'pages/classic-register';
} else {
    $data['upload_data'] = $this->upload->data();
    // use custom function to determine if filetype is allowed
    if (allow_file_type($data['upload_data']['file_ext'])) 
    {
        $filename = $data['upload_data']['file_name'];
    }
    else
    {
        show_error('File type is not allowed!');
    }
}

I've had these same problems with CI and haven't been able to find a fix on the forums or via google. What I've done is to allow all filetypes, so that the file gets uploaded. Then, I handle the logic manually to determine whether to allow/keep the file, or delete it and tell the user that filetype is not allowed.

Until I find something that works, this is the only solution that works for me at present. Tried modifying config/mimes.php and system/libraries/Upload.php, to no avail. I guess I can post-process file types to make sure they are legitimate types.

Note
Rectangle 27 1

php Uploading in Codeigniter The filetype you are attempting to upload is not allowed?


'The filetype you are attempting to upload is not allowed'

Solution, not sure, but quick fix that appears to work, change condition of line 1032 to NOT so that it reads: if (!function_exists('finfo_file')) instead of: if (function_exists('finfo_file')). Hope this can help someone.

This is for Codeigniter version 2.2. If this question is still relevant. I traced the fault in file system/libraries/upload.php file to function: protected function _file_mime_type($file) at line 1032 and line 1046: $this->file_type = $matches[1]; When I was uploading a file with extension .txt the statement at line 1046 seems to assign an incorrect value of 'text/x-asm' to $this->file_type which is later compared to 'text/plain' and since mime types do not match the test fails and signals an inappropriate file type and error message:

Note
Rectangle 27 1

php Uploading in Codeigniter The filetype you are attempting to upload is not allowed?


$this->file_type = @mime_content_type($file['tmp_name']);
if (strlen($this->file_type) > 0) return;
@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_name']), $output, $return_code);

As you can probably see, line 1058 tries to use an array value that does not exist.

Basically what I did was modify a few lines of code in the File Upload Class (Location: ./system/libraries/Upload.php)

If you're using Codeigniter version 2.1.0 there is a bug in the Upload library. See http://codeigniter.com/forums/viewthread/204725/ for more details.

helped a lot ... :)

Note
Rectangle 27 1

php Uploading in Codeigniter The filetype you are attempting to upload is not allowed?


'docx' => array('application/zip', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')
application/config/mimes.php
do_upload()
system/libraries/Upload.php
var_dump($this->file_type);
exit();
var_dump()

Little example: you have problem with .docx. Try to add:

Note
Rectangle 27 1

php Uploading in Codeigniter The filetype you are attempting to upload is not allowed?


Note
Rectangle 27 1

php Uploading in Codeigniter The filetype you are attempting to upload is not allowed?


So here in the following link, I have described what it really takes to get through this problem and figure out the solution!

The same problem I had with, when I was trying to do an Upload form to upload, ".xlsx" files, which in CodeIgniter's mimes.php file has an entry in it's array, to represent "xlsx" file extensions.

Note
Rectangle 27 1

php Uploading in Codeigniter The filetype you are attempting to upload is not allowed?


'The filetype you are attempting to upload is not allowed'

Solution, not sure, but quick fix that appears to work, change condition of line 1032 to NOT so that it reads: if (!function_exists('finfo_file')) instead of: if (function_exists('finfo_file')). Hope this can help someone.

This is for Codeigniter version 2.2. If this question is still relevant. I traced the fault in file system/libraries/upload.php file to function: protected function _file_mime_type($file) at line 1032 and line 1046: $this->file_type = $matches[1]; When I was uploading a file with extension .txt the statement at line 1046 seems to assign an incorrect value of 'text/x-asm' to $this->file_type which is later compared to 'text/plain' and since mime types do not match the test fails and signals an inappropriate file type and error message:

Note