【问题标题】:upload image in codeigniter (ERROR)在codeigniter中上传图片(错误)
【发布时间】:2017-10-04 04:32:22
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

  • 你能把错误贴在这里吗?
  • 实际上在我上传图片后,无论上传图片是否成功,这个编码应该重定向到'purchase_v1/dashboard/page/a29'。但它只显示一个空白页,所以我不知道这是什么错误。如果 ($this->upload->do_upload('fileImg'))
  • 您是否为上传文件的文件夹分配了文件权限?此外,启用 error_reporting 以便您查看错误。
  • 如何启用.error_reporting?

标签: php image codeigniter upload


【解决方案1】:

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

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

} 

【讨论】:

    猜你喜欢
    • 2019-02-02
    • 2019-01-02
    • 2019-04-27
    • 2017-05-28
    • 2013-01-11
    • 2021-01-30
    • 2018-01-03
    • 2018-12-17
    • 1970-01-01
    相关资源
    最近更新 更多