2011-09-21 47 views
15

我收到錯誤:當我嘗試上傳任何文件時,不允許使用您嘗試上傳的文件類型。在Codeigniter中上傳 - 您嘗試上傳的文件類型不允許

if(!empty($_FILES['proof_of_purchase']['name'])) { 
    $config['upload_path'] = './uploads/invoices/'; 
    $config['allowed_types'] = 'gif|jpg|jpeg|png|pdf|bmp'; 
    $config['max_size'] = '3000'; 
    $this->load->library('upload', $config); 

     // if there was an error, return and display it 
    if (!$this->upload->do_upload('proof_of_purchase')) 
    { 
     $data['error'] = $this->upload->display_errors(); 
     $data['include'] = 'pages/classic-register'; 
    } else { 
     $data['upload_data'] = $this->upload->data(); 
     $filename = $data['upload_data']['file_name']; 
    } 
} 

我已經嘗試了許多不同的文件 - 主要是GIF & JPEG,每次得到同樣的錯誤。

var_dump($ _ FILES);給我:

array(1) { ["proof_of_purchase"]=> array(5) { ["name"]=> string(28) "2010-12-04_00019.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(19) "D:\temp\php2BAE.tmp" ["error"]=> int(0) ["size"]=> int(58054) } } 

我檢查了mime配置,它包含正確的東西。例如:

'jpeg' => array('image/jpeg', 'image/pjpeg'), 
'jpg' => array('image/jpeg', 'image/pjpeg'), 
'jpe' => array('image/jpeg', 'image/pjpeg'), 

我在這上面花了太多時間,這讓我感到非常瘋狂!任何想法都會非常有幫助。

+2

嘗試使用'$ this-> upload-> data()'檢查CodeIgniter讀取的dile信息,可能你會發現一些線索。 –

+0

看起來好嗎? - 「array(14){[」file_name「] => string(15)」minifur-hs1.jpg「[」file_type「] => string(10)」image/jpeg「[」file_path「] => string( 32)「D:/ www/website/uploads/invoices /」[「full_path」] => string(47)「D:/www/website/uploads/invoices/minifur-hs1.jpg」[「raw_name」] = > string(11)「minifur-hs1」[「orig_name」] => string(0)「」[「client_name」] => string(15)「minifur-hs1.jpg」[「file_ext」] => string( 4)「.jpg」[「file_size」] => int(18168)[「is_image」] => bool(true)[「image_width」] => string(0)「」[「image_height」] => string( 0)「」[「image_type」] => string(0)「」[「image_size_str」] => string(0)「」} – dangermark

+0

我也遇到了這個問題。當我使用「'時出現錯誤。在形式的另一個領域,但它似乎工作正常,只要我不使用'。'奇怪的。 –

回答

25

如果您使用笨版本2.1。 0上傳庫中有一個錯誤。有關更多詳情,請參閱http://codeigniter.com/forums/viewthread/204725/

基本上我所做的修改幾行代碼在文件上傳類(地點:./system/libraries/Upload.php)從

1)修改行號1044

$this->file_type = @mime_content_type($file['tmp_name']); 
return; 

這樣:

$this->file_type = @mime_content_type($file['tmp_name']); 
if (strlen($this->file_type) > 0) return; 

2)修改的行號1058

來自:

@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_path']), $output, $return_code); 

這樣:

@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_name']), $output, $return_code); 

正如你很可能會看到,線路1058嘗試使用一個不存在的數組值。

+0

幫了很多...... :) – JayKandari

4

我對CI有這些相同的問題,一直沒有找到修復論壇或通過谷歌。我所做的是允許所有文件類型,以便文件上傳。然後,我手動處理邏輯以確定是否允許/保留該文件,或刪除它並告訴用戶文件類型不被允許。

$config['upload_path'] = './uploads/invoices/'; 
$config['allowed_types'] = '*'; // add the asterisk instead of extensions 
$config['max_size'] = '3000'; 
$this->load->library('upload', $config); 

if (!$this->upload->do_upload('proof_of_purchase')) 
{ 
    $data['error'] = $this->upload->display_errors(); 
    $data['include'] = 'pages/classic-register'; 
} else { 
    $data['upload_data'] = $this->upload->data(); 
    // use custom function to determine if filetype is allowed 
    if (allow_file_type($data['upload_data']['file_ext'])) 
    { 
     $filename = $data['upload_data']['file_name']; 
    } 
    else 
    { 
     show_error('File type is not allowed!'); 
    } 
} 

編輯 - 這是假設你使用CI 2(在CI 1,你可以按照教程這裏允許所有文件類型:http://net.tutsplus.com/tutorials/php/6-codeigniter-hacks-for-the-masters/

+0

直到我找到有用的東西,這是目前唯一適用於我的解決方案。試圖修改config/mimes.php和system/libraries/Upload.php,無濟於事。我想我可以後處理文件類型,以確保它們是合法的類型。 – TARKUS

1

我有同樣的問題。您可能需要檢查應用程序是否識別正在上傳的文件的mimetype。添加一個新的mimetype到config/mimes.php修復了這個問題。 :-)

0

解決方法是更換upload.php的系統/庫/由笨V2.0.3的upload.php的

3

我所做的就是建立自己的圖書館「MY_Upload」延長CI_Upload類,然後我複製了CI_Upload類,並在我的自定義庫中應用由Adam概述的更改(感謝解決方案中的一堆BTW)。

這樣我就可以使用標準的CI語法,避免黑客的原始文件!我的圖書館是自動使用的,因爲它只是「擴展」原來的,這是一個完全無痛的解決方案,不會因爲某些原因必須替換原始文件而中斷。

PS:我這樣做與記錄類也爲當我想要生成自定義日誌。

0

此問題是由不具有PHP的FileInfo擴展造成的。上傳類使用的功能是該擴展的一部分。

http://www.php.net/manual/en/ref.fileinfo.php

+0

我剛剛在我的CPanel中找到了擴展,並啓用了它。嘗試上傳.png時仍然遇到「您嘗試上傳的文件類型不允許」的問題。 – TARKUS

0

如果您不希望更改的系統文件。剛剛嘗試這一點:

  1. 打開system/libraries/Upload.php(方法​​3210線205),併爲此

    var_dump($this->file_type); 
    exit(); 
    
  2. 嘗試上傳一些文件。

  3. 添加文件類型,您在var_dump()中看到的文件類型爲application/config/mimes.php

小例子:你有問題.docx。嘗試添加:

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

application/config/mimes.php

1

這是笨2.2版。如果這個問題仍然相關。我追溯文件系統/庫/ upload.php文件中的錯誤功能:protected function _file_mime_type($file)在1032行和1046行:$this->file_type = $matches[1]; 當我上傳一個擴展名爲.txt的文件時,1046行的語句似乎分配了一個不正確的值'text/x-asm'$this->file_type稍後相比'text/plain'以來MIME類型不測試失敗匹配和信號有關的不適當的文件類型和錯誤消息:

'The filetype you are attempting to upload is not allowed'

解決方案,不知道,但快速解決出現的工作,改變條件1032行至NOT,以便它讀取:if (!function_exists('finfo_file'))而不是:if (function_exists('finfo_file'))。希望這可以幫助某人。

0

當我嘗試執行上載表單上傳時,遇到了同樣的問題, 「.xlsx」文件(在CodeIgniter的mimes.php文件中有一個條目在其數組中,表示「xlsx」文件擴展。所以在這裏

在以下link,我描述了什麼是真正需要通過這個問題來獲得並找出解決方案!

最好的問候,

Randika

0

這也許值得一試,你有最新版本的應用程序/配置/ mimes.php作爲,而不是設置變量$默劇,現在只是返回數組。

mimes.php

返回數組

mimes.php

$啞劇=陣列

如果您仍然有舊版本的mimes.php的Common.php函數& get_mimes()返回一個空數組。 這有突破upload.php的

一次我跟蹤這個的knockon起,所有工作正常:)

0

1)允許所有文件類型。 2)手動設置驗證規則與傳統的PHP接受或拒絕文件類型。 3)如果驗證規則服從,則使用CI上傳助手上傳。

if (isset($_FILES['upload_file']) && !empty($_FILES['upload_file'] ['name'])) { 
     $file_name=$_FILES['upload_file'] ['name']; 
     $acceptedext=array("zip","pdf","png","jpg","jpeg"); 
     $a=(explode('.', $file_name)); 
     $b=end($a); 
     $file_ext=strtolower($b); 

     if (!in_array($file_ext, $acceptedext)) { 
      $this->session->set_flashdata('flash_message_error', "file format not accepted!"); 
      redirect(base_url('page')); 
     } 
     else{ 
      $config['upload_path'] = './uploads/'; 
      $config['allowed_types'] = '*'; 


      $this->load->library('upload'); 
      $this->upload->initialize($config); 

      if (!$this->upload->do_upload('upload_file')) { 

       $error = $this->upload->display_errors('', ' '); 
       $this->session->set_flashdata('flash_message_error', $error); 
       redirect(base_url('page')); 
      } } } 
相關問題