简体   繁体   English

在codeigniter中上传图片(错误)

[英]upload image in codeigniter (ERROR)

I'm trying to create an image upload coding using CodeIgniter and save it on my target location. 我正在尝试使用CodeIgniter创建图像上传编码,并将其保存在我的目标位置。 This coding works fine in localhost however when I tried to upload this coding to Cpanel it gives me an error. 该编码在localhost中可以正常工作,但是当我尝试将此编码上传到Cpanel时,出现了错误。

Controller Code: 控制器代码:

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');
    }
} 

view Code: 查看代码:

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

You have a wrong initialization of the upload library. 您上载库的初始化错误。 Use it like this. 像这样使用它。 If this does not solve the problem, then it is probably not a problem with uploading. 如果这不能解决问题,则上传可能不是问题。 It might be an issue somewhere else in the code. 这可能是代码中其他地方的问题。 The code itself is very poor. 代码本身非常糟糕。

  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');
    }

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM