6

i want to show the success message of a content insert on a redirect link.here is my code of controller:-

public function do_upload($field) {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100000';
        $config['max_width'] = '3000';
        $config['max_height'] = '3000';

        $this->load->library('upload', $config);

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

            return $error;
        } else {

            $updata =$this->upload->data();
            $data = $updata['raw_name'].$updata['file_ext'];
            return $data;
        }


     }


public function index() {

 $this->load->model('blog');

        if(isset($_POST['post']) && (!empty($_POST['post']) || strlen($_FILES['inputUpProfile']['name']) > 0) ){
        if(strlen($_FILES['inputUpProfile']['name']) > 0) 
        {
        $pic = $this->do_upload('inputUpProfile');
        print_r($pic);
        if ($this->input->post('post') == ''){$type="image";} else {$type="image-with-text";}
        }

        else {$pic = ""; $type = "text"; }

            $result = $this->blog->addPost($_SESSION['user_id'], $type  , $this->input->post('post'),$pic);
        }
$this->template->build('home_view',$this->data);
}

when upload image big than 3000 its was show me this message

Array ( [error] =>

The filetype you are attempting to upload is not allowed.
) 

how can i read this error in my view ??

================================================

Update :-

the error messagelike this:-

Array ( [error] =>

The filetype you are attempting to upload is not allowed.
)
A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: mysql/mysql_driver.php

Line Number: 552
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at E:\AppServ\www\cc\ccc\application\controllers\home.php:83)

Filename: core/Common.php

Line Number: 442
A Database Error Occurred

Error Number: 1054

Unknown column 'Array' in 'field list'

INSERT INTO `events` (`ev_user_id`, `ev_type`, `ev_text`, `ev_pic`, `ev_date`) VALUES (445, 'image', '', Array, '2013-10-03 23:51:47')

Filename: E:\AppServ\www\cc\ccc\system\database\DB_driver.php

Line Number: 330

3 Answers 3

14

if you got error like

Array ( [error] =>
   The filetype you are attempting to upload is not allowed.
)

in the controller you can do like

if (!$this->upload->do_upload($field)) {
    $error = array('error' => $this->upload->display_errors());
    $this->session->set_flashdata('error',$error['error']);
    redirect('your_function_which_loads_the_view','refresh');
}

and in the view page do something like

if($this->session->flashdata('error')){echo $this->session->flashdata('error');}

please let me know if you face any problem.

Sign up to request clarification or add additional context in comments.

2 Comments

hello sir, what if multiple images are getting uploded and some non image type file is also added to the files. CI actually gives error, work fine but some files are getting uploded in this process. what should i do to check all file at first in CI?
$this->session->set_flashdata('error',$this->upload->display_errors()); would be just fine as well, but remember the function by default encapsulates the text with <p>..</p>, so display_errors( '', '' ) might be useful to know about.
1

just display the error in your view, here the steps: first load session library, do it at folder config, file aoutoload.php

$autoload['libraries'] = array('session');

in the controller as you have, change this piece of code for this

  if (!$this->upload->do_upload($field)) {
    $this->session->set_flashdata( 'error_msg', $this->upload->display_errors() );
    redirect( base_url( 'your view' ) );
  }

in your view file...

    <?php
    if ( $this->session->flashdata( 'error_msg' ) ) {
        echo $this->session->flashdata('error_msg');
    }
    ?>

1 Comment

Obrigado amigo.
0
$pic = $this->do_upload('inputUpProfile');
$this->data['upload_error'] = $pic['error'];

Then in the view:

echo $upload_error;

1 Comment

You have 3 errors there 2 of which are in your model which you did not provide the code for. The first error tells you exactly what is wrong: the type of file is not allowed. This is a configuration which can be set. Please read the user guide: ellislab.com/codeigniter/user-guide

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.