【问题标题】:Undefined property: Main::$upload , Codeigniter未定义的属性: Main::$upload , Codeigniter
【发布时间】:2019-03-01 18:53:54
【问题描述】:

我正在尝试使用 codeigniter 上传单个图像。但是出现错误 "未定义的属性:Main::$upload

文件名:controllers/Main.php

行号:14"

这是我的控制器文件“controller/Main.php”

<?php

class Main extends CI_Controller
{
    public function index(){
        $this->load->view('main_view', array('error' => ''));
    }

    public function upload(){
        $config['upload_path'] = "./images/";
        $config['allowed_types'] = 'jpg|png|gif';
        $this->load->initialize('upload', $config);

        if(!$this->upload->do_upload()){
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('main_view', $error);
        }
        else {
            $file_data = $this->upload->data();
            $data['img'] = base_url().'/images/'.$file_data['file_name'];
            $this->load->view('success_msg', $data);
        }
    }
}
?>

这是我的视图文件“views/main_view.php”

<!DOCTYPE html>
<html>
<head>
<title>Image</title>
</head>
<body>
    <?php echo $error; ?>
    <?php echo form_open_multipart('main/upload');?>
    <input type="file" name="userfile" />
    <input type="submit" name="Submit" value="upload image"/>
    </form>
</body>
</html>

当图片上传成功时,我试图通过这个“view/success_msg.php”显示图片

<!DOCTYPE html>
<html>
<head>
    <title>Image</title>
</head>
<body>
    <h1>File has been uploaded.</h1>
    <img src="<?php $img?>" width="300" height="300">
</body>
</html>

我的图像文件夹名称是“images”,它位于根目录中。请帮助我为什么会出现此错误?

【问题讨论】:

    标签: codeigniter image-upload


    【解决方案1】:

    可能没有加载上传库。将public function upload()的前几行改成如下

    public function upload(){
        $config['upload_path'] = "./images/";
        $config['allowed_types'] = 'jpg|png|gif';
        $this->load->library('upload', $config);
    

    通过将$config 发送到load-&gt;library,您无需使用initialize()

    【讨论】:

      【解决方案2】:

      控制器

        public function upload(){
                      $cc = $_FILES['userfile']['name'];
      
                      $extension = pathinfo($cc, PATHINFO_EXTENSION);
                      $newfile = preg_replace('/[^A-Za-z0-9]/', "", $_FILES["userfile"]['name']);
                      $config['file_name'] = time() . $newfile;
                      $ss = ".";
                      $picture1 = time() . $newfile . $ss . $extension;
                      $config['upload_path'] = "./images/";
                      $config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
                      $this->load->library('upload', $config);
                      if (!$this->upload->do_upload('userfile')) {
                          echo $this->upload->display_errors();
                      } else {
                          $this->load->view('success_msg', $picture1 );
                      }
                 } 
      

      查看页面

      <!DOCTYPE html>
      <html>
      <head>
      <title>Image</title>
      </head>
      <body>
          <?php echo $error; ?>
          <?php echo form_open_multipart('main/upload');?>
          <input type="file" name="userfile" />
          <input type="submit" name="Submit" value="upload image"/>
          </form>
      </body>
      </html>
      

      查看/success_msg.php

      <!DOCTYPE html>
      <html>
      <head>
          <title>Image</title>
      </head>
      <body>
          <h1>File has been uploaded.</h1>
          <img src="<?php $picture1 ?>" width="300" height="300">
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-20
        相关资源
        最近更新 更多