Неопределенное свойство: Main :: $ upload, Codeigniter - PullRequest
0 голосов
/ 01 марта 2019

Я пытаюсь загрузить одно изображение с помощью 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 голосов
/ 02 марта 2019

Контроллер

  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>
0 голосов
/ 01 марта 2019

Возможно, загружаемая библиотека не загружена.Измените первые несколько строк 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().

...