Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:55:11+00:00 2026-06-17T16:55:11+00:00

i am trying to show image upload error message using flashdata but the code

  • 0

i am trying to show image upload error message using flashdata but the code which i shown below is not working fine. What could be the reason?

Please suggest me a solution to solve this issue.

mycontrollerPage.php

class Booksetups extends CI_Controller  
{
    function book($book_id = 0)
    {
       $config = array(); 
       $config['base_url'] = 'http://localhost/thexxr.com/Booksetups/book/pgn/';
       $config["total_rows"] = $this->Booksmodel->record_count(); 
       $config['uri_segment'] = 4; 
       $config['per_page'] = 5;  
       $config['full_tag_open'] = '<div id="pagination">';
       $config['full_tag_close'] = '</div>';

        //------------not wroking file upload error validation------------------------------------------   
        $config['upload_path'] = 'uploads/'; 
        $config['allowed_types'] = 'gif|jpg|jpeg|png'; 
        $config['max_size'] = '1000'; 
        $config['max_width'] = '1920'; 
        $config['max_height'] = '1280'; 
        $config['remove_spaces'] = 'TRUE'; 
        $config['file_path'] = 'TRUE'; 
        $config['full_path']    = '/uploads/';

        $this->load->library('upload', $config);
        $this->upload->do_upload("img1");
        $this->upload->do_upload("img2");
        //------------------------------------------------------------------------


       $this->pagination->initialize($config);   
       $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;   

       $this->form_validation->set_error_delimiters('<div  class="error">', '</div>')->set_rules('subject_id', 'subject_id','trim|required|min_length[1]|max_length[150]|xss_clean');
       $this->form_validation->set_error_delimiters('<div  class="error">', '</div>')->set_rules('cover_id', 'cover_id','trim|required|min_length[1]|max_length[150]|xss_clean');
       $this->form_validation->set_error_delimiters('<div  class="error">', '</div>')->set_rules('language_id', 'language_id','trim|required|min_length[1]|max_length[150]|xss_clean');
       $this->form_validation->set_error_delimiters('<div  class="error">', '</div>')->set_rules('edition_id', 'edition_id','trim|required|min_length[1]|max_length[150]|xss_clean');

        if(($this->input->post('book_id'))&& ($this->form_validation->run() === TRUE))
        {  
            if( ! $this->upload->do_upload()) //trying to display error
            {
                   $error = array('error' => $this->upload->display_errors()); 
                   $this->session->set_flashdata('error', $error);
                   redirect(current_url()); 
            }
            $this->session->set_flashdata('msg', '1 row(s) affected.');
            $this->Booksmodel->entry_update();  
            redirect(current_url());
        }
    }
}       

Myview.php

<?php echo '<fieldset><legend>'. $formtopic.'</legend>' ?>  

<?php echo validation_errors();  ?>   
<?php 
    $message = $this->session->flashdata('msg');  
    if($message)
    {
    ?>
    <div class="success" id="msgdiv"><?php echo  $this->session->flashdata('msg'); ?></div> 
    <?php 
    }
?>  

<?php 
$message = $this->session->flashdata('error');  
    if($message)
    {
    ?> 
    <?php echo $error;?>   
    <!-- Here i am getting. Message like "array" why not i am getting the error message instead? --> 
<?php 
    }
?> 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:55:12+00:00Added an answer on June 17, 2026 at 4:55 pm

    I will recommend using form_validation’s validation_errors(). Here is how i do it.
    Take a look at the library in this answer.
    This library has two methods validate_upload (it only checks if file is valid)
    and do_upload(must be used only when validate_upload returns true).

    There is a file upload library available in Codeigniter Here is the documentation.
    Copy the code of my answer and paste it in a file called MY_Upload.php and save this file in application/Code folder.
    And now i define a rule like this

    $this->form_validation->set_rules('userfile', 'New Image', 'trim|callback_valid_upload');   
    

    When the image upload is optional you can wrap it in a condition

    if(isset($_FILES['userfile']) AND $_FILES['userfile']['name']!= ''){
        $this->form_validation->set_rules('userfile', 'New Image', 'trim|callback_valid_upload');   
    }   
    

    Where userfile is file field of form.
    You can see i have called a function in rule callback_valid_upload
    Here is method of controller used in callback

    public function valid_upload()
    {
        $this->load->library('upload'); 
    
        $config['upload_path']      =   'your path here';           
        $config['allowed_types']    =   'png';
        $config['max_size']         =   2048;
        $config['max_width']        =   85;
        $config['max_height']       =   110;
    
        $this->upload->initialize($config);
    
        if (!$this->upload->validate_upload('userfile'))
        {
            $this->form_validation->set_message('valid_upload', $this->upload->display_errors());
            return FALSE;
        }else{
            return TRUE;
        }       
    }
    

    This method will check if file we are uploading is valid and will return true on success else false.
    When validation is failed load form

    View
    
    echo validation_errors();
    //blah blah
    //<form action="">
    //  <input type="" />
    //  <input type="" />
    //  <input type="" />
    //</form>
    

    And if you want to display the messages seperatly

    <input type="file" name="userfile" id="" /> 
    <?php echo form_error('userfile')?>
    

    And if validation is successfull upload file now like this.

    if($this->form_validation->run())
    {
        if(isset($_FILES['userfile']) AND $_FILES['userfile']['name'] !='')
        {
            $this->upload->do_upload('userfile'); // this will upload file
            $image_data =   $this->upload->data();
            $data['image_field_name_of_table']      =   $image_data['raw_name'];
        }
        //other data in $data array here
    
        $id =   $this->mymodel->insert($data);
        if($id){
            $this->session->set_flashdata('notice', ' Successful');
            redirect('your url');   
        }
    }else{
        //load view here
    }    
    

    MORE EDITS :
    One thing i noticed in your code is a problem

       $config = array(); 
       $config['base_url'] = 'http://localhost/thexxr.com/Booksetups/book/pgn/';
       $config["total_rows"] = $this->Booksmodel->record_count(); 
       $config['uri_segment'] = 4; 
       $config['per_page'] = 5;  
       $config['full_tag_open'] = '<div id="pagination">';
       $config['full_tag_close'] = '</div>';
    
        $this->load->library('upload', $config);
        $this->upload->do_upload("img1");
    
        unset($config);
    
        $config['upload_path'] = 'uploads/'; 
        $config['allowed_types'] = 'gif|jpg|jpeg|png'; 
        $config['max_size'] = '1000'; 
        $config['max_width'] = '1920'; 
        $config['max_height'] = '1280'; 
        $config['remove_spaces'] = 'TRUE'; 
        $config['file_path'] = 'TRUE'; 
        $config['full_path']    = '/uploads/';
    
        $this->load->library('upload', $config); //load again with new configuration
        $this->upload->do_upload("img1");
        $this->upload->do_upload("img2");   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

iam trying to show image preview before upload, for that i am using code
hi i'm trying to upload a image from an iframe and is working, but
I am trying to show a histogram for a image in jlabel, but its
This is the code I have. I'm trying to insert a image to show
I'm trying to develop image crop using JQuery. I use ajax to upload the
I am trying to upload the text with image using FBRequestConnection like as follow
I'm trying to upload an image using jQuery and PHP. I know there are
The below is the runtime exeception which i got while am trying to upload
I am trying to post image on facebook but not successful yet, my codes
currently I'm trying to get multi upload in my admin interface working. I'm using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.