【问题标题】:CodeIgniter "The filetype you are attempting to upload is not allowed."CodeIgniter "您尝试上传的文件类型不被允许。"
【发布时间】:2012-04-22 03:52:01
【问题描述】:

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

我正在尝试上传 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' =&gt; array('image/png', 'image/x-png'), 行。

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

感谢您的每一次帮助尝试。

【问题讨论】:

    标签: php codeigniter mime-types image-uploading


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      另一种解决方案是在php.ini

      中启用extension=php_fileinfo.dll

      【讨论】:

        【解决方案3】:

        只需编辑 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')
        

        【讨论】:

        • 有 10 个,不是我的文件类型 (mp3),但可以帮我解决问题
        【解决方案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;  
        
            }  
        
        }  
        

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            【解决方案6】:

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

            【讨论】:

            • 谢谢 - 这解决了我的问题。我的问题与 CSV 文件直接相关,并带有相同的错误消息。
            【解决方案7】:

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

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

            【讨论】:

              猜你喜欢
              • 2017-08-18
              • 2012-04-06
              • 1970-01-01
              • 2011-11-21
              • 2011-06-08
              • 2014-12-30
              • 2023-04-07
              • 2016-11-14
              • 1970-01-01
              相关资源
              最近更新 更多