未定义的属性:Main :: $ upload,Codeigniter

时间:2019-03-01 18:53:55

标签: codeigniter image-upload

我正在尝试使用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”,它在根目录中。请帮我为什么我会出现此错误?

2 个答案:

答案 0 :(得分:1)

也许没有加载上传库。将public function upload()的前几行更改为以下

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

通过向$config发送load->library,您不需要使用initialize()

答案 1 :(得分:0)

控制器

  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>

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