没有上载图像大小调整器并调整其大小

时间:2019-07-16 09:30:52

标签: php html codeigniter web

我有问题。为什么当我上传文件时未上传。并显示“请检查您的图像!” ?

这是针对使用框架Codeigniter的PHP

公共功能Submit_image(){

    $input_name = $_POST['input_name'];
    $input_email = $_POST['input_email'];
    $input_code = $_POST['input_code'];

    $config['file_name'] = $input_code;
    $config['overwrite'] = TRUE;
    $config['upload_path']          = './img/';
    $config['allowed_types']        = 'gif|jpg|png|jpeg';
    $config['max_size']             = 5000000;
    //$config['max_width']            = 6000;
    //$config['max_height']           = 4000;

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

  if(!empty($_FILES['filepicture']['name'])){

    if ($input_gambar = $this->upload->do_upload('doc')) {
    $data = $upload_data = $this->upload->data();

    //Compress Image


    $config['image_library'] = 'gd2';
    $config['source_image'] = './img/'.$data['file_name'];
    $config['create_thumb'] = FALSE;
    $config['maintain_ratio'] = FALSE;
    $config['quality'] = '50%';
    $config['width'] = 6000;
    $config['height'] = 4000;
    $config['new_image'] = './img/resized_'.$data['file_name'];
    $this->load->library('image_lib', $config);
    if (!$this->image_lib->resize()) {
        show_error($this->image_lib->display_errors());
    }
        echo "<script>alert('Thanks for upload Image');</script>";
        echo "<script>window.history.go(-2);</script>";            
    }else{
        echo "<script>alert('Please check your image!');window.history.go(-1);</script>";
    }
  }              
}

2 个答案:

答案 0 :(得分:0)

您应该检查display_errors()中是否有上传库,以查看问题所在:

function submit_image() {
    $input_name = $this->input->post('input_name');
    $input_email = $this->input->post('input_email');
    $input_code = $this->input->post('input_code');
    if (is_null($input_code)) {
        show_error('Input code missing.');
    }
    $config['file_name'] = $input_code;
    $config['overwrite'] = TRUE;
    $config['upload_path'] = './img/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg'; // removed pdf
    $config['max_size'] = 5000000; // this is dependent on your server which will override this value
    if (!empty($_FILES['filepicture']['name'])) {
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if ($this->upload->do_upload('filepicture')) { // changed 'doc' to filepicture which seems to be the name of your field
            $data = $this->upload->data();
            $config['image_library'] = 'gd2';
            $config['source_image'] = './img/' . $data['file_name'];
            $config['create_thumb'] = FALSE;
            $config['maintain_ratio'] = FALSE;
            $config['quality'] = '50%';
            $config['width'] = 6000;
            $config['height'] = 4000;
            $config['new_image'] = './img/resized_' . $data['file_name'];
            $this->load->library('image_lib', $config);
            if (!$this->image_lib->resize()) {
                show_error($this->image_lib->display_errors());
            }
            echo "<script>alert('Thanks for upload Image');</script>";
            echo "<script>window.history.go(-2);</script>";
        } else {
            show_error($this->upload->display_errors());
        }
    }
}

您还必须在原始代码filepicturedoc中上载字段的名称冲突。我不确定它实际上是哪一个。 do_upload('field_name')要求field_name作为上载字段的属性的确切名称。

答案 1 :(得分:0)

 Just update this - $this->upload->do_upload('filepicture') //file name should come here



   $input_name = $_POST['input_name'];
    $input_email = $_POST['input_email'];
    $input_code = $_POST['input_code'];

    $config['file_name'] = $input_code;
    $config['overwrite'] = TRUE;
    $config['upload_path']          = './img/';
    $config['allowed_types']        = 'gif|jpg|png|jpeg';
    $config['max_size']             = 5000000;
    //$config['max_width']            = 6000;
    //$config['max_height']           = 4000;

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

  if(!empty($_FILES['filepicture']['name'])){

    if ($input_gambar = $this->upload->do_upload('filepicture')) {
    $data = $upload_data = $this->upload->data();

    //Compress Image


    $config['image_library'] = 'gd2';
    $config['source_image'] = './img/'.$data['file_name'];
    $config['create_thumb'] = FALSE;
    $config['maintain_ratio'] = FALSE;
    $config['quality'] = '50%';
    $config['width'] = 6000;
    $config['height'] = 4000;
    $config['new_image'] = './img/resized_'.$data['file_name'];
    $this->load->library('image_lib', $config);
    if (!$this->image_lib->resize()) {
        show_error($this->image_lib->display_errors());
    }
        echo "<script>alert('Thanks for upload Image');</script>";
        echo "<script>window.history.go(-2);</script>";            
    }else{
        echo "<script>alert('Please check your image!');window.history.go(-1);</script>";
    }
  }              
}