在codeigniter中上传图像(ERROR)

时间:2017-05-05 15:57:52

标签: php image codeigniter upload

我正在尝试使用CodeIgniter创建图像上传编码并将其保存在我的目标位置。这个编码在localhost中运行正常但是当我尝试将这个编码上传到Cpanel时,它给了我一个错误。

控制器代码:

public function uploadPaid(){

    if ($this->input->post()) {                    

        $arr = $this->input->post();                
        $this->load->database();
        $this->load->model('m_picture');
        $this->load->model('m_purchase');
        //$this->load->library('my_func');
        //$this->load->helper('url');
        $this->load->library('upload');                    


        $config = array(
                'upload_path' => "./dist/invoice/",
                'allowed_types' => "gif|jpg|png",
                'overwrite' => TRUE,
                'max_size' => "2000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
                'max_height' => "0",
                'max_width' => "0",
                'encrypt_name' => true
            );

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

        $pur_id = $this->input->post('pur_id');
        $img_background = $this->input->post('fileImg');

        if ($this->upload->do_upload('fileImg'))
        {
            $data = $this->upload->data();
            $background="dist/invoice/".$data['raw_name'].$data['file_ext'];
            echo $background;
            $arr2 = array(                                  
                    "img_url" => $background,
                    "ne_id" => $arr['pur_id']                                  
                );           

            $this->m_purchase->updateInv(1, $pur_id);
            $this->m_picture->insert($arr2);
            $this->session->set_flashdata('success' , '<b>Well done!</b> You successfully send the picture.');
            redirect(site_url('purchase_v1/dashboard/page/a29'),'refresh');
        }
        else 
        {
            echo $this->upload->display_errors();
            $this->session->set_flashdata('warning' , '<b>Error!</b> You failed to send the picture.');
            redirect(site_url('purchase_v1/dashboard/page/a29'),'refresh');
        }
    }else{
        $this->session->set_flashdata('warning' , '<b>Uh Crap!</b> You got Error. The image size is to big');
        redirect(site_url('purchase_v1/dashboard/page/a29'),'refresh');
    }
} 

查看代码:

<form action="<?= site_url('purchase_v1/dashboard/uploadPaid'); ?>"  method="POST" role="form" enctype="multipart/form-data">
    <div class="portlet-body flip-scroll" align="center">
        <span style = "color : #b706d6;"><h2><strong>#<?= (110000+$pur_id); ?></strong></h2></span>
        <div class="form-group">
            <div class="fileinput fileinput-new" align="center" data-provides="fileinput">
                <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px; line-height: 150px;"></div>
                <div>
                    <span class="btn btn-outline btn-file" style="background-color: #FF5733">
                        <span class="fileinput-new"> Select image </span>
                        <span class="fileinput-exists"> Change </span> 
                        <input type="hidden" value="" name="title"><input type="file" name="fileImg"> 
                    </span>
                    <a href="javascript:;" class="btn red fileinput-exists" data-dismiss="fileinput"> Remove </a>
                </div>

                <div class="clearfix">&nbsp;</div>
                <button type="submit" class="btn btn-primary"><i class="fa fa-upload"> Submit</i></button>
            </div>
        </div>
    </div>
    <input type="hidden" name="pur_id" id="pur_id" class="form-control" value="<?= $pur_id; ?>">
</form>

1 个答案:

答案 0 :(得分:0)

您的上传库初始化错误。像这样使用它。如果这不能解决问题,那么上传可能不是问题。它可能是代码中其他地方的问题。代码本身很差。

  if ($this->input->post()) {

        $arr = $this->input->post();                
        $this->load->database();
        $this->load->model('m_picture');
        $this->load->model('m_purchase');
        //$this->load->library('my_func');
        //$this->load->helper('url');
        $this->load->library('upload');

        $config = array(
        'upload_path' => "./dist/invoice/",
        'allowed_types' => "gif|jpg|png",
        'overwrite' => TRUE,
        'max_size' => "2000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
        'max_height' => "0",
        'max_width' => "0",
        'encrypt_name' => true
        );

        $this->upload->initialize($config);

        $pur_id = $this->input->post('pur_id');
        $img_background = $this->input->post('fileImg');


        if ($this->upload->do_upload('fileImg'))
        {
            $data = $this->upload->data();
            $background="dist/invoice/".$data['raw_name'].$data['file_ext'];
            echo $background;
         $arr2 = array(
                    "img_url" => $background,
                    "ne_id" => $arr['pur_id'] 
                );           

        $this->m_purchase->updateInv(1, $pur_id);
        $this->m_picture->insert($arr2);
        $this->session->set_flashdata('success' , '<b>Well done!</b> You successfully send the picture.');
        redirect(site_url('purchase_v1/dashboard/page/a29'),'refresh');
        }
        else 
        {
        echo $this->upload->display_errors();
        $this->session->set_flashdata('warning' , '<b>Error!</b> You failed to send the picture.');
        redirect(site_url('purchase_v1/dashboard/page/a29'),'refresh');
        }

    }else
    {
        $this->session->set_flashdata('warning' , '<b>Uh Crap!</b> You got Error. The image size is to big');
        redirect(site_url('purchase_v1/dashboard/page/a29'),'refresh');
    }

}