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

问题描述 投票:0回答:2

我正在尝试使用codeigniter上传单个图像。但得到错误“Undefined property:Main :: $ upload

文件名:controllers / Main.php

行号:14“

This is my controller file "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);
        }
    }
}
?>

and this is my view file "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>

when image upload is successful I'm trying to show the image by this "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
2个回答
1
投票

可能没有加载上传库。将public function upload()的前几行更改为以下内容

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

通过发送$configload->library你不需要使用initialize()


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>

视图/ 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>
© www.soinside.com 2019 - 2024. All rights reserved.