Uploading in Codeigniter - The filetype you are at

2019-01-04 13:13发布

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'),

I've spent far too long on this and it's driving me nuts! Any ideas would be extremely helpful.

11条回答
forever°为你锁心
2楼-- · 2019-01-04 13:31

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

查看更多
看我几分像从前
3楼-- · 2019-01-04 13:34

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

查看更多
别忘想泡老子
4楼-- · 2019-01-04 13:37

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

查看更多
Explosion°爆炸
5楼-- · 2019-01-04 13:40

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

查看更多
何必那么认真
6楼-- · 2019-01-04 13:42

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.

查看更多
我只想做你的唯一
7楼-- · 2019-01-04 13:45

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/)

查看更多
登录 后发表回答