【问题标题】:image upload in codeigniter giving error在codeigniter中上传图像给出错误
【发布时间】:2015-10-29 04:54:44
【问题描述】:

我在我的表单中上传了一张图片,但它总是给我一个错误,比如

“遇到 PHP 错误 严重性:通知消息: 未定义索引:vimage 文件名:controllers/vouchers.php 行 编号:42"

我的查看文件代码:

<?php echo form_open_multipart(site_url("promotions/add_item/"), array("class" => "form-horizontal","id"=>"addItem")) ?>
                            <div class="form-group">
                              <label class="col-sm-3 control-label">Show on</label>
                              <div class="col-sm-3">
                                Artist Directory : <input type="checkbox" name="artist_dir" value='1'>
                              </div>  
                              <div class="col-sm-3">
                                Highlight : <input type="checkbox" name="highlight" value='1'>
                              </div>
                            </div>
                        
                            <div class="form-group">
                              <label class="col-sm-3 control-label">Title</label>
                              <div class="col-sm-9">
                                <input type="text" class="form-control" name="title" id="title" placeholder="Title">
                              </div>
                            </div>
                             <div class="form-group">
                              <label class="col-sm-3 control-label">Image</label>
                              <div class="col-sm-9">
                                  <input type="file" name="pro_image">
                              </div>
                            </div>
                            <div class="form-group">
                              <label class="col-sm-3 control-label">Description</label>
                              <div class="col-sm-9">
                                  <textarea class="form-control" rows="3" name="description" placeholder="Description"></textarea>
                              </div>
                            </div>
                            <div class="form-group">
                              <div class="col-sm-offset-3 col-sm-9">
                                  <button type="submit" class="btn btn-success" name="submit" id="submit">Submit</button>
                              </div>
                            </div>
                          
                        <?php echo form_close() ?>

我的控制器代码:

<pre>
 public function add_item(){
        
            $this->load->library('upload');
             
            $this->form_validation->set_rules('title','Title','required');
            $this->form_validation->set_rules('description','Description','required');
            
            print_r($_FILES['pro_image']); 
            if ($_FILES['pro_image']['size'] > 0) {
                    $this->upload->initialize(array( 
                    "upload_path" => './uploads/',
                    "overwrite" => FALSE,
                    "encrypt_name" => TRUE,
                    "remove_spaces" => TRUE,
                    "allowed_types" => "gif|jpg|png|jpeg",
                ));


                 if (!$this->upload->do_upload('vimage')) {
                     $this->upload->display_errors();
                 }

                 $data = $this->upload->data();
                 echo 'img : '.$img = $data['file_name'];
            } 
            exit;
            
            if($this->form_validation->run()==FALSE){
                echo '<div class="alert alert-dismissable alert-danger"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><small>'.  validation_errors().'</small></div>';
            }
            else {
                $title = $this->input->post('title');
                $description = $this->input->post('description');
                $artist_dir = $this->input->post('artist_dir');
                $highlight = $this->input->post('highlight');
                $this->promotions_model->add_item($title, $description,$artist_dir,$highlight);
            }
    }
    </pre>

请让我知道我在这里做错了什么

【问题讨论】:

  • 你加载库upload了吗?
  • var_dump($_FILES); 显示了什么?
  • 是的,我添加了上传库 $this->load->library('upload');当我做 var_dump($_FILES);它向我显示 null

标签: php codeigniter file-upload


【解决方案1】:

试试这个

  if ($_FILES['vimage']['size'] > 0) {
    $this->load->library('upload');
     $this->upload->initialize($this->set_upload_options());
                    $this->upload->do_upload();
                    $fileName = $_FILES['vimage']['name'];

    private function set_upload_options()
      { 
      // upload an image options
             $config = array();
             $config['upload_path'] = './upload/'; //give the path to upload the image in folder
             $config['allowed_types'] = 'gif|jpg|png';
              $config['max_size'] = '0';
             $config['overwrite'] = FALSE;
      return $config;
      }

更多信息请阅读Link

【讨论】:

    【解决方案2】:

    试试这个

    if (!empty($_FILES['vimage']))
    {
        $config['upload_path'] = './uploads/';
        //$config['max_size'] = '102400';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['overwrite'] = FALSE;
        $config['remove_spaces'] = TRUE;
        $config['encrypt_name'] = TRUE;
    
        $this->load->library('upload', $config);
        $this->upload->initialize($config);
    
        if (!$this->upload->do_upload('vimage'))
        {
            $this->session->set_flashdata('error', $this->upload->display_errors());
            //redirect('controller/method');
        }
        else
        {
            $this->session->set_flashdata('success', 'Image Has been Uploaded');
            //redirect('controller/method');
            $img = $data['file_name'];
        }
    
    }
    

    【讨论】:

    • 嗨,当我尝试此代码时,它给了我一个错误,例如“严重性:通知消息:未定义变量:img 文件名:controllers/vouchers.php 行号:60”和第 60 行是:$ img = $data['file_name'];这个
    • 好的,那么我如何在数据库中添加图像路径?你能指导我吗
    • 只添加图片名称。并在视图中&lt;img src="&lt;?php echo base_url() ?&gt;.'uploads/'.&lt;?php echo #### ?&gt; "
    • ### 将是存储在数据库中的图像的图像名称
    • 我试过了,但还是不上传
    【解决方案3】:

    请先检查,

    print_r(_FILES);

    如果你得到文件数组,那么你可以使用下面的代码来上传图片。

    function do_upload()
    {
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']    = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    
    // You can give Image formats if you want to upload any video file.
    
    $this->load->library('upload', $config);
    
    if ( ! $this->upload->do_upload())
    {
    $error = array('error' = $this->upload->display_errors());
    
    // uploading failed. $error will holds the errors.
    }
    else
    {
    $data = array('upload_data' => $this->upload->data());
    
    // uploading successfull, now do your further actions
    }
    }
    

    【讨论】:

    • 嗨,我在 print_r(_FILES);它即将为空
    • 所以问题出在您的表单中,请检查您的表单,您的控制器中没有从表单中获取任何数据。
    • 在我的表单中,我还有 3 个其他字段,因为我正在获取值,我可以插入到数据库中,但只有在这个字段中我才会这样
    • 你能在这里分享你所有模型、视图和控制器的代码吗?
    • 你能给我你的email id,我可以发给你吗?
    【解决方案4】:

    我已经在我的系统中添加了你的代码,它对我来说很好。

    public function add(){
            $this->load->library('upload');
            $this->load->helper('form');
            $this->load->helper('url');
             print_r($_FILES);die; 
    
                $this->form_validation->set_rules('title','Title','required');
                $this->form_validation->set_rules('description','Description','required');
    
    
                if ($_FILES['pro_image']['size'] > 0) {
                        $this->upload->initialize(array( 
                        "upload_path" => './uploads/',
                        "overwrite" => FALSE,
                        "encrypt_name" => TRUE,
                        "remove_spaces" => TRUE,
                        "allowed_types" => "gif|jpg|png|jpeg",
                    ));
    
    
                     if (!$this->upload->do_upload('vimage')) {
                         $this->upload->display_errors();
                     }
    
                     $data = $this->upload->data();
                     echo 'img : '.$img = $data['file_name'];
                } 
                exit;
    
                if($this->form_validation->run()==FALSE){
                    echo '<div class="alert alert-dismissable alert-danger"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><small>'.  validation_errors().'</small></div>';
                }
                else {
                    $title = $this->input->post('title');
                    $description = $this->input->post('description');
                    $artist_dir = $this->input->post('artist_dir');
                    $highlight = $this->input->post('highlight');
                    $this->promotions_model->add_item($title, $description,$artist_dir,$highlight);
                }
    
    
        }
    

    【讨论】:

    • 我仍然遇到同样的错误,当我执行 print_r($_FILES) 时,它即将为空。有什么问题我必须在配置中设置什么?
    • 现在请尝试使用简单的表单更改表单,例如
    • 你好,我已经按照这个做了,但我仍然收到同样的错误
    • 我遇到了问题,我在模式窗口中打开了表单,所以
      如果我删除 "id="addItem" 然后图片上传工作正常,但我的表单验证不起作用表单我们无法添加表单 ID,您能给我任何想法吗
    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 2017-10-04
    • 2014-12-08
    • 1970-01-01
    • 2018-12-17
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多