如何在Codeigniter中上传多张图片?

时间:2015-09-24 11:40:26

标签: codeigniter upload

现在我正在尝试用户图像(user_pic,background_image)upoload。 但我的代码不起作用。 我应该做些不同的事吗?不确定出了什么问题。

这是我的HTML代码

<form action="<?=base_url();?>edit/up_profile/" method="post" enctype="multipart/form-data">
<input type="file" name="pic_file">
<input type="file" name="pic_bgfile" />
<button type="submit">

我的控制器代码是

if($this->form_validation->run()  ===  false){
}else{
  $config = array(
    'upload_path' => "./images/u/photo",
    'allowed_types' => "gif|jpg|png|jpeg|pdf",
    'overwrite' => TRUE,
    'max_size' => "2048000",
    'max_height' => "768",
    'max_width' => "1024"
  );
  $this->load->library('upload',$config);
  if($this->upload->do_upload('pic_file')){
    $data['profile_image'] = $this->upload->data();
  }else if($this->upload->do_upload('pic_bgfile')){
    $data['pic_bgfile'] = $this->upload->data();
  }
  $this->load->model('user_model');
  $data = array(
    'user_id'   =>    $this->session->userdata('user_id'),
    'info_tit'    =>    $this->input->post('info_tit'),
    'scope'   =>    $this->input->post('scope')
  );
  $this->user_model->p_update($data);
  //redirect
  redirect('/edit/profile/', 'location', 301);
}

3 个答案:

答案 0 :(得分:0)

您应该使用CodeIgniter自己的上传类,而不是使用HTML代码。

在视图中,或者更确切地说,是.html文件:

<?php 
  echo form_open_multipart('user/upload_picture');
?>
<input type="file" name="userfile" />
<br /><br />
<input type="submit" value="submit" />
</form>

注意到在'user / upload_picture'中,'user'是控制器的名称,'upload_picture'是'user'中的方法。

在'upload_picture'方法中:

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg';
$config['max_size'] = 0;
$config['max_width']  = 0;
$config['max_height']  = 0;
$config['file_name'] = 'Apink.jpg';
$this->load->library('upload', $config);
$this->upload->do_upload();

答案 1 :(得分:0)

观点:

<html>
<head>
<title>Upload Form</title>  
</head>
<body>
<?php echo $error;?> <!-- Error Message will show up here -->
<?php echo form_open_multipart('upload_controller/do_upload');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
</body>
</html>

控制器

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
        class Upload_Controller extends CI_Controller {
        public function __construct() {
        parent::__construct();
        }
        public function file_view(){
        $this->load->view('file_view', array('error' => ' ' ));
        }
        public function do_upload(){
        $config = array(
        'upload_path' => "./uploads/",
        'allowed_types' => "gif|jpg|png|jpeg|pdf",
        'overwrite' => TRUE,
        'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
        'max_height' => "768",
        'max_width' => "1024"
        );
        $this->load->library('upload', $config);
        if($this->upload->do_upload())
        {
        $data = array('upload_data' => $this->upload->data());
        $this->load->view('upload_success',$data);
        }
        else
        {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('file_view', $error);
        }
        }
        }
        ?>

注意:您可以根据要上传的文件更改首选项。

显示上传文件的页面:

<html>
        <head>
        <title>Upload File Specification</title>
        </head>
        <body>
        <h3>Your file was successfully uploaded!</h3>
        <!-- Uploaded file specification will show up here -->
        <ul>
        <?php foreach ($upload_data as $item => $value):?>
        <li><?php echo $item;?>: <?php echo $value;?></li>
        <?php endforeach; ?>
        </ul>
        <p><?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?></p>
        </body>
        </html>

答案 2 :(得分:0)

首次上传图像后,您可以使用清除功能     未设置($配置);
    $这 - &GT; image_lib-&GT;清除();

参见CI手册
https://codeigniter.com/user_guide/libraries/image_lib.html