2012-04-09 52 views
6

我在寻找很多东西,发现了很多关于这个问题的问题,遗憾的是没有任何答案对我有帮助。CodeIgniter“您正试图上传的文件类型是不允许的。”

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

The filetype you are attempting to upload is not allowed.

我这个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

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

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

回答

1

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

0

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

7

的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') 
+0

有云10,我不是一个类型的文件(MP3),但帮我解决我的问题 – 2017-01-31 00:47:17

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

    } 

} 
0

我刚加入这一行中第34行上的mime.php和pptx现在正在上传。测试真实服务器上

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

4

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

+0

谢谢 - 这解决了我的问题。我的问题直接与CSV文件相关,并具有相同的错误消息。 – 2014-12-04 11:37:44

相关问题