2017-07-25 61 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);