Codeigniter:通过Web服务上传图片

时间:2017-07-25 13:23:11

标签: php codeigniter

我正在尝试通过网络服务上传一张图片。以下是代码

  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>";

请检查打印的两个回复,让我知道如何解决这个问题。

2 个答案:

答案 0 :(得分:0)

试试这个。这可能会有所帮助

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

答案 1 :(得分: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);