16

I am getting the error: The filetype you are attempting to upload is not allowed when I try to uplaod any file.

if(!empty($_FILES['proof_of_purchase']['name'])) {
    $config['upload_path'] = './uploads/invoices/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png|pdf|bmp';
    $config['max_size'] = '3000';
    $this->load->library('upload', $config);
  
      // if there was an error, return and display it
    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();
        $filename = $data['upload_data']['file_name'];
    }
}

I have tried many different files- mostly gif & jpeg and get the same error each time.

var_dump($_FILES); gives me:

array(1) { ["proof_of_purchase"]=> array(5) { ["name"]=> string(28) "2010-12-04_00019.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(19) "D:\temp\php2BAE.tmp" ["error"]=> int(0) ["size"]=> int(58054) } } 

I have checked the mime config and it contains the right stuff. Example:

'jpeg'  =>  array('image/jpeg', 'image/pjpeg'),
'jpg'   =>  array('image/jpeg', 'image/pjpeg'),
'jpe'   =>  array('image/jpeg', 'image/pjpeg'),
Nimantha
  • 6,405
  • 6
  • 28
  • 69
dangermark
  • 544
  • 2
  • 5
  • 17
  • 2
    try using `$this->upload->data()` to check dile info as read by CodeIgniter, chances are you find some clue there. – Damien Pirsy Sep 21 '11 at 07:01
  • Looks ok to me? - `array(14) { ["file_name"]=> string(15) "minifur-hs1.jpg" ["file_type"]=> string(10) "image/jpeg" ["file_path"]=> string(32) "D:/www/website/uploads/invoices/" ["full_path"]=> string(47) "D:/www/website/uploads/invoices/minifur-hs1.jpg" ["raw_name"]=> string(11) "minifur-hs1" ["orig_name"]=> string(0) "" ["client_name"]=> string(15) "minifur-hs1.jpg" ["file_ext"]=> string(4) ".jpg" ["file_size"]=> int(18168) ["is_image"]=> bool(true) ["image_width"]=> string(0) "" ["image_height"]=> string(0) "" ["image_type"]=> string(0) "" ["image_size_str"]=> string(0) "" }` – dangermark Sep 21 '11 at 22:51
  • I'm also getting this problem. I get the error when I use '.' in another field of the form but it seems to work fine whenever I don't use the '.' Weird. – el_pup_le Sep 24 '11 at 20:37
  • +1 I get this problem all the time. Sometimes it works, sometimes it doesn't. – MacMac Dec 15 '11 at 15:12
  • @dangermark -- any chance the web server is running PHP v5.2? I had this same problem after upgrading to the latest CodeIgniter and that turned out to be the issue for me. Downgrading to CodeIgniter v2.0.3 resolved it. – stealthyninja Dec 15 '11 at 19:05

11 Answers11

25

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.

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

1) modify Line number 1044

from:

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

to this:

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

2) modify line number 1058

from:

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

to this:

@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.

Adam
  • 613
  • 1
  • 8
  • 10
5

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.

$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!');
    }
}

EDIT - This is assuming you're using CI 2 (in CI 1 you can follow the tutorial here to allow all filetypes: http://net.tutsplus.com/tutorials/php/6-codeigniter-hacks-for-the-masters/)

swatkins
  • 13,530
  • 4
  • 46
  • 78
  • 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. – TARKUS Nov 05 '13 at 03:37
3

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.

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.

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

Petar Zivkovic
  • 970
  • 10
  • 20
1

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:

'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.

Tom Tom
  • 3,680
  • 5
  • 35
  • 40
Jason
  • 11
  • 2
1

1) Allow all filetypes. 2) Manually set validation rule with traditional php to accept or reject file types. 3) if validation rules are obeyed, then upload using CI upload helper.

if (isset($_FILES['upload_file']) && !empty($_FILES['upload_file'] ['name'])) {
        $file_name=$_FILES['upload_file'] ['name'];
        $acceptedext=array("zip","pdf","png","jpg","jpeg");
        $a=(explode('.', $file_name));
        $b=end($a);
        $file_ext=strtolower($b);

        if (!in_array($file_ext, $acceptedext)) {
            $this->session->set_flashdata( 'flash_message_error', "file format not accepted!" );
            redirect( base_url( 'page' ) );
        }
        else{
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = '*';


            $this->load->library('upload');
            $this->upload->initialize($config);

            if (!$this->upload->do_upload('upload_file')) {

                $error = $this->upload->display_errors('', ' ');
                $this->session->set_flashdata( 'flash_message_error', $error );
                redirect( base_url( 'page' ) );
            } } }
1

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. :-)

abhisek
  • 924
  • 1
  • 13
  • 27
0

The solution is replace the Upload.php in the system/libraries/ by Upload.php of CodeIgniter v2.0.3

0

This problem is caused by not having the PHP FileInfo extension. The function the upload class uses is part of that extension.

http://www.php.net/manual/en/ref.fileinfo.php

Andrew
  • 421
  • 1
  • 5
  • 11
  • I just found that extension in my CPanel, and enabled it. Still get the problem of "the file type you are attempting to upload is not allowed" when trying to upload a .png. – TARKUS Apr 09 '16 at 14:54
0

If you don't want to change system files. Just try this:

  1. Open system/libraries/Upload.php(method do_upload() line 205) and do this

    var_dump($this->file_type);
    exit();
    
  2. Try to upload some file.

  3. Add file type, which you see in var_dump() to application/config/mimes.php.

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

'docx' => array('application/zip', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')

To application/config/mimes.php.

joni jones
  • 2,865
  • 3
  • 23
  • 28
0

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.

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

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Randika Vishman
  • 7,983
  • 3
  • 57
  • 80
0

It's probably worth checking that you have the latest version of the application/config/mimes.php as instead of setting a variable $mimes it now simply returns an array.

new mimes.php

return array

old mimes.php

$mimes = array

If you still have the old version of mimes.php the Common.php function &get_mimes() returns an empty array. This has the knockon effect of breaking Upload.php

Once I traced this, all was working fine :)