如何在codeigniter中上传文件

时间:2017-03-14 06:10:00

标签: php arrays codeigniter

我使用codeigniter,我想在codeigniter中创建文件上传功能,我已经编写了代码但是它接受了所有类型的文件,它也没有显示错误按摩。

这是我的查看页面HTML代码:文件名是:welcome_message.php

<div class="form-group">
            <input type = "file" name = "userprofile" size = "20" required="">
        </div>

这是我的控制器代码:文件名是:Welcome.php

       public function index()  
    {   
         $this->load->view('welcome_message', 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('Welcome', $config);

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

                        $this->load->view('welcome_message', $error);
                }
                else
                {
                        $data = array('upload_data' => $this->Welcome->data());

                        $this->load->view('upload_success', $data);
                }
        }

我的成功消息文件:文件名:upload_success.php:

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

<h3>Your file was successfully uploaded!</h3>

<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>

<p><?php echo anchor('Welcome', 'Upload Another File!'); ?></p>

</body>
</html>

2 个答案:

答案 0 :(得分:0)

你需要改变这个

                $error = array('error' => $this->Welcome->display_errors();

到这个

                $error = array('error' => $this->upload->display_errors();
当你没有任何错误时

同样

   $data = array('upload_data' => $this->Welcome->data());

   $data = array('upload_data' => $this->upload->data());

您需要回显错误才能显示它。 现在检查

  public function index()  
    {   
         $this->load->view('welcome_message', 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('Welcome', $config);

                if ( ! $this->upload->do_upload('my_n_file')) //just use your name here
                {
                        $error = array('error' => $this->upload->display_errors());

                        $this->load->view('welcome_message', $error);
                }
                else
                {
                        $data = array('upload_data' => $this->upload->data());

                        $this->load->view('upload_success', $data);
                }
        }

查看文件以显示错误

  <html>
    <head>
    <title>Upload Form</title>
    </head>
    <body>    
   <form >
<input type="file" name="my_n_file"> // just use your name here
<?=isset($error)?$error:"";?>
<button type="submit">submit</button>

</form>
    </body>
    </html>

答案 1 :(得分:0)

您没有包含错误显示位置的变量。你必须添加

<?php echo $error;?>

还要使用

检查html中的文件
accept="image/*"

希望这有帮助!