Php 如何在codeigniter中进行文件上传

Php 如何在codeigniter中进行文件上传,php,arrays,codeigniter,Php,Arrays,Codeigniter,我正在使用codeigniter,我想在codeigniter中实现文件上传功能,我已经编写了代码,但它接受所有类型的文件,并且没有显示错误信息 这是我的查看页面HTML代码:文件名是:welcome_message.php <div class="form-group"> <input type = "file" name = "userprofile" size = "20" required=""> </div>

我正在使用codeigniter,我想在codeigniter中实现文件上传功能,我已经编写了代码,但它接受所有类型的文件,并且没有显示错误信息

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

<div class="form-group">
            <input type = "file" name = "userprofile" size = "20" required="">
        </div>
我的成功消息文件:文件名: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>

上传表单
您的文件已成功上载!
  • :


您需要对此进行更改

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

                $error = array('error' => $this->upload->display_errors();
当你没有任何错误时,这里也一样

   $data = array('upload_data' => $this->Welcome->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>

上传表单
//在这里用你的名字
提交

您没有包含将显示错误的变量。你必须加上

<?php echo $error;?>

希望这有帮助

表单元素在哪里?首先告诉你想要上传什么类型的文件。您也可以使用accept=“image/*”限制html端的文件,然后上传文件的配置将有所帮助。我需要接受这种类型的$config['allowed_types']='gif | jpg | png'@raja您是否再次检查了我的答案?您的视图中没有表单标记?我看到了使用表单帮助程序,我像那样做了更改,但没有显示所有类型的文件都出现了错误。@raja检查此项now@raja观点不同。这是你的表单所在的视图
  <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>
<?php echo $error;?>
accept="image/*"