0

now i'm trying user images(user_pic,background_image) upoload. but my codes not working. should i be doing something different? Not really sure what's going wrong.

Here my html code

<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">

my controller code is

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);
}
  • If you are trying to upload multiple images codeigniter is only made to upload one image at a time But this link may help http://stackoverflow.com/questions/11524356/multiple-files-upload-array-with-codeigniter-2-0 –  Sep 24 '15 at 12:11

3 Answers3

0

You should use CodeIgniter's own upload class instead of using html code.

In the view, or rather, the .html file:

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

Noted that in 'user/upload_picture', 'user' is the name of the controller, 'upload_picture' is the method in 'user'.

In the 'upload_picture' method:

$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();
0

The View :

<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>

The Controller

<?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);
        }
        }
        }
        ?>

Note : You can change preferences depending upon the file you wish to upload.

The Page to show uploaded file:

<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>
Rajan
  • 2,427
  • 10
  • 51
  • 111
0

You can use clear function after first image upload
unset($config);
$this->image_lib->clear();

See CI Manual
https://codeigniter.com/user_guide/libraries/image_lib.html