2012-04-09 53 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

相關問題