php CodeIgniter “不允许您尝试上传的文件类型。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10078885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:20:17  来源:igfitidea点击:

CodeIgniter "The filetype you are attempting to upload is not allowed."

phpcodeignitermime-typesimage-uploading

提问by Cyclone

I was searching a lot and found many questions regarding this problem, unfortunately none of answers did help me.

我搜索了很多,发现了很多关于这个问题的问题,不幸的是没有一个答案对我有帮助。

I'm trying to upload a png image, and I'm receiving the following error:

我正在尝试上传 png 图像,但收到以下错误:

The filetype you are attempting to upload is not allowed.

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

I was following this CI guide to build my code: http://codeigniter.com/user_guide/libraries/file_uploading.html

我按照这个 CI 指南来构建我的代码:http: //codeigniter.com/user_guide/libraries/file_uploading.html

Here is what I got:

这是我得到的:

view file:

查看文件:

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

My controller:

我的控制器:

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

As you can see I'm tryin to var_dumpthe mime type and result is empty.

如您所见,我正在尝试var_dump使用 mime 类型,结果为空。

When I do var_dump($_FILES)it looks like everything is fine:

当我这样做时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) } }

Also, I got 'png' => array('image/png', 'image/x-png'),line, in my config/mimes.php.

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

However, it does happend for all images (haven't tried yet other extensions).

但是,它确实发生在所有图像上(还没有尝试过其他扩展)。

I'd appreciate every help attempt.

我很感激每一次帮助尝试。

回答by Slipstream

Just edit the mimes.phpfile in application/config/mimes.phpand replace the line for the csvby this one:

只需编辑application/config/mimes.php 中mimes.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')

回答by Vandana

Add ‘text/plain' to the CSV array in config/mimes.php to $mimes arrays

将 'text/plain' 添加到 config/mimes.php 中的 CSV 数组到 $mimes 数组

回答by nani1216

Replacing the codeigniter 2.1.0 system/libraries/upload.php with 2.1.2 version upload.php library solves the problem. Hope this helps

用 2.1.2 版本的 upload.php 库替换 codeigniter 2.1.0 system/libraries/upload.php 解决了这个问题。希望这可以帮助

回答by Miomir Dancevic

$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;  

    }  

}  

回答by khubbaib Naeem

i just add this line in mime.php on line 34 and pptx is now uploading. test it on real server

我只是在第 34 行的 mime.php 中添加了这一行,现在正在上传 pptx。在真实服务器上测试

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

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

回答by Mo Hechanova

another solution is to enable extension=php_fileinfo.dllin php.ini

另一种解决方案是extension=php_fileinfo.dllphp.ini 中启用

回答by Nguyen Tan Dat

Actually, in my case, I created a blob object from a canvas by void canvas.toBlob(callback, mimeType, qualityArgument)method So the blob file does NOT have its real name (which has an extension), it's just only 'blob'. So I have to use the legacy way to upload the file, instead of an 'upload' library:

实际上,就我而言,我通过void canvas.toBlob(callback, mimeType, qualityArgument)方法从画布创建了一个 blob 对象,因此 blob 文件没有其真实名称(具有扩展名),它只是“blob”。所以我必须使用传统方式上传文件,而不是“上传”库:

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