【问题标题】:Error Showing on image upload in codeigniter在 codeigniter 中上传图片时显示错误
【发布时间】:2019-04-27 05:47:43
【问题描述】:
        public function set_news($id = 0)
    {
     $this->load->helper('url'); 
    $slug = url_title($this->input->post('title'), 'dash', TRUE);
            
     $name=$_FILES["myimage"]["name"];
            
     $folder="./uploads/";
    $imageFileType = strtolower(pathinfo($name,PATHINFO_EXTENSION));
    $extensions_arr = array("jpg","jpeg","png");    

    if( in_array($imageFileType, $extensions_arr) )
    {                  
    $data = array(
    'title' => $this->input->post('title'), //         $this->db->escape($this->input->post('title'))
    'date' => $this->input->post('date'),
    'slug' => $slug,
    'text' => $this->input->post('text'),
    'myimage' => $name,
    'user_id' => $this->input->post('user_id'),
    );
   move_uploaded_file($_FILES["myimage"]["tmp_name"],         "$folder".$_FILES["myimage"]["name"]);    
    }
    //  else
    //   {
    //   echo "<script>alert('Image Error');window.history.back();        </script>";
    // }             
            
    if ($id == 0) {
    //$this->db->query('YOUR QUERY HERE');
    return $this->db->insert('news', $data);
    } else {
    $this->db->where('id', $id);
    return $this->db->update('news', $data);
    }
    }

这是我的控制器文件

上传图片时显示错误如下

遇到了 PHP 错误

严重性:通知

消息:未定义索引:myimage

文件名:models/News_model.php

行号:48

回溯:

文件:C:\xampp\htdocs\web\codeigniter\application\models\News_model.php 线路:48 函数:_error_handler

文件:C:\xampp\htdocs\web\codeigniter\application\controllers\News.php 线路:123 功能:set_news

文件:C:\xampp\htdocs\web\codeigniter\index.php 线路:315 函数:require_once

【问题讨论】:

  • 向我们展示 var_dump($_FILES); 的结果.. 并确保文件输入名称是 myimage。
  • 您还没有向我们展示您的 HTML - 您的表单中真的有名称为 myimage 的文件上传吗?

标签: codeigniter


【解决方案1】:

您似乎错过了将输入名称设置为“myimage”。这是完整的codeigniter图像上传代码(在他们的文档中提到)。

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo $error;?>

<?php echo form_open_multipart('upload/do_upload');?>

<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

</form>

</body>
</html>

控制器代码:

class Upload extends CI_Controller {

        public function __construct()
        {
                parent::__construct();
                $this->load->helper(array('form', 'url'));
        }

        public function index()
        {
                $this->load->view('upload_form', array('error' => ' ' ));
        }

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

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

                if ( ! $this->upload->do_upload('userfile'))
                {
                        $error = array('error' => $this->upload->display_errors());

                        $this->load->view('upload_form', $error);
                }
                else
                {
                        $data = array('upload_data' => $this->upload->data());
                          $uploadData= array(
                         'title' => TITLE,
                        'date' => date("Y-m-d H:i:s"),
                        'slug' => SLUG,
                        'text' => TEXT,
                        'myimage' => FILE_NAME,
                        'user_id' => USER_ID,
                       );
                      $this->db->insert('mytable', $uploadData);
                        $this->load->view('upload_success', $data);
                }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 2020-12-31
    • 2019-02-02
    • 2012-03-05
    • 2018-12-17
    • 1970-01-01
    • 2019-01-02
    相关资源
    最近更新 更多