2017-07-25 133 views
0

我想通過網絡服務上傳一張圖片。以下是代碼Codeingiter:通過網絡服務上傳圖片

public function upload() { 
     $config['upload_path']   = './uploads/'; 
     $config['allowed_types'] = 'gif|jpg|png|mp4|jpeg'; 
     $config['max_size']    = 100; 
     $config['max_width']   = 1024; 
     $config['max_height']   = 768; 
     $this->load->library('upload', $config); 
     $this->upload->initialize($config); 
     $this->data['data']= $_FILES; 
     echo json_encode($this->data); die; 

     if (! $this->upload->do_upload('userfile')) 
     { 
       $error = array('error' => $this->upload->display_errors()); 
       $this->data['data']= $error ; 
       echo json_encode($this->data['data']); 
       die; 
     } 
     else 
     { 
       $data = array('upload_data' => $this->upload->data()); 
             $this->data['data']= 'done' ; 

       echo json_encode($this->data['data']); 
       die; 
     } 
} 

如果我json_encode($_FILES)這是在移動

data =  { 
     userfile =   { 
      error = 0; 
      name = pen; 
      size = 38238; 
      "tmp_name" = "/tmp/phpEsEQNK"; 
      type = jpeg; 
     }; 
    }; 

響應當我打印錯誤陣列,這是我所得到的

error = "<p>The filetype you are attempting to upload is not allowed.</p>"; 

請檢查印刷兩種反應並讓我知道我該如何解決這個問題。

+0

檢查以下門票:https://stackoverflow.com/questions/9815208/codeigniter-the-filetype-you-are-attempting-to-upload-is - 不允許 –

回答

0

試試這個。這可能有助於

$config["allowed_types"] = "image/jpeg|image/gif|image/jpg|image/png|video/mp4"; 
+0

背後的邏輯是什麼? –

0

轉到 系統/庫/ upload.php的

然後找出行號199,

$this->_file_mime_type($_FILES[$field]); 

更改該行,

$this->_file_mime_type($_FILES[$field]); var_dump($this->file_type); die(); 

嘗試在這裏檢查MIME類型。現在

,如果您使用的是CI 2.1.0版本,那麼在上傳庫中的缺陷,

轉到:/system/libraries/Upload.php(行號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; 

查找:(行號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);