笨“您尝试上传文件类型是不允许的。”(CodeIgniter “The filetype you a

2019-06-23 11:37发布

我在寻找了很多,发现关于这个问题的许多问题,遗憾的是没有答案的确实帮助我。

我想上传一个PNG图像,并且我收到以下错误:

您尝试上传的文件类型是不允许的。

我下面这个CI指南建立我的代码: http://codeigniter.com/user_guide/libraries/file_uploading.html

下面是我得到了什么:

查看文件:

[..]
   <?= form_open_multipart() ?>
   <input type="file" name="userfile" size="20" />
   <br /><br />
   <input type="submit" value="upload" />
   <?= form_close() ?>
[..]

我的控制器:

    $config['upload_path']   = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = '100';
    $config['max_width']     = '1024';
    $config['max_height']    = '768';


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

        $xx = array('upload_data' => $this->upload->data());
        $mimetype= $xx['upload_data']['file_type'];

        var_dump('Mime: ' . $mimetype);
        var_dump($_FILES);

        if ( !$this->upload->do_upload())
        {
            Notice::add($this->upload->display_errors(), 'error');
        }
        else
        {
            $data['upload_data'] = $this->upload->data();
        }

正如你可以看到我试着var_dump MIME类型和结果为空。

当我做var_dump($_FILES)看起来一切都很好:

array(1) { ["userfile"]=> array(5) { ["name"]=> string(14) "imageofm.png" ["type"]=> string(9) "image/png" ["tmp_name"]=> string(18) "/var/tmp/php5cDAZJ" ["error"]=> int(0) ["size"]=> int(358) } }

另外,我'png' => array('image/png', 'image/x-png'),线,在我的config/mimes.php

但是,它happend所有图像(没试过另一些扩展)。

我很感激每一个帮助的尝试。

Answer 1:

只是编辑的application / config / mimes.phpmimes.php文件,并通过这一个更换为CSV行:

'csv' => array('application/vnd.ms-excel', 'text/anytext', 'text/plain', 'text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel')


Answer 2:

添加“text / plain的”到CSV阵列中配置/ mimes.php至$哑剧阵列



Answer 3:

与2.1.2版本的upload.php的库更换笨2.1.0系统/库/ upload.php的解决了这个问题。 希望这可以帮助



Answer 4:

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

$this->upload->set_allowed_types('*'); 


class MY_Upload extends CI_Upload {  

    function is_allowed_filetype() {  

        if (count($this->allowed_types) == 0 OR ! is_array($this->allowed_types))  
        {  
            $this->set_error('upload_no_file_types');  
            return FALSE;  
        }  

        if (in_array("*", $this->allowed_types))  
        {  
            return TRUE;  
        }  

        $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe');  

        foreach ($this->allowed_types as $val)  
        {  
            $mime = $this->mimes_types(strtolower($val));  

            // Images get some additional checks  
            if (in_array($val, $image_types))  
            {  
                if (getimagesize($this->file_temp) === FALSE)  
                {  
                    return FALSE;  
                }  
            }  

            if (is_array($mime))  
            {  
                if (in_array($this->file_type, $mime, TRUE))  
                {  
                    return TRUE;  
                }  
            }  
            else  
            {  
                if ($mime == $this->file_type)  
                {  
                    return TRUE;  
                }  
            }  
        }  

        return FALSE;  

    }  

}  


Answer 5:

另一种解决方案是使extension=php_fileinfo.dllphp.ini



Answer 6:

我只是在mime.php加上线34和PPTX现在上传这条线。 测试真实服务器

'PPTX'=>数组( '应用/ vnd.openxmlformats-officedocument.wordprocessingml.document', '应用程序/压缩'),



Answer 7:

其实,在我的情况,我通过创建从一个帆布的Blob对象void canvas.toBlob(callback, mimeType, qualityArgument)方法所以BLOB文件没有它的真名(其中有一个扩展名),它只是唯一的“斑点” 。 所以,我必须使用传统的方式上传,而不是一个“上传”库文件,:

move_uploaded_file ( $file["tmp_name"], $target_file )


文章来源: CodeIgniter “The filetype you are attempting to upload is not allowed.”