【问题标题】:Codeigniter: Image upload through web servicesCodeigniter:通过网络服务上传图片
【发布时间】:2017-12-31 11:04:41
【问题描述】:

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

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

请检查打印的两个回复,并告诉我如何解决这个问题。

【问题讨论】:

标签: php codeigniter


【解决方案1】:

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

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

【讨论】:

  • 这背后的逻辑是什么?
【解决方案2】:

转到 系统/库/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); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多