CodeIgniter “不允许使用您尝试上传的文件类型


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

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

我正在尝试上传 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) } }

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

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

我将不胜感激每一次帮助尝试。

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

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

将 codeigniter 2.1.0 系统/库/上传.php替换为 2.1.2 版本上传.php库解决了这个问题。希望这有帮助

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

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

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

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

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

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