Welcome Guest, Not a member yet? Register   Sign In
How to display disallowed file type error in case of upload in Codeigniter 3?
#1

(This post was last modified: 01-03-2020, 07:12 PM by Ajax30.)

I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.

The posts, of course, have main images. There is a [i]default image[/i] if no image is uploaded by the user but, if an image [i]is[/i] uploaded, there are only 3 types allowed: jpg, jpeg and png.


Wanting to warn the user in case she/he tries to upload other file formats, I did this in the Posts controller:



Code:
// Upload image
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';

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

if(!$this->upload->do_upload()){

    $data['uerrors'] = $this->upload->display_errors();

    if ($data['uerrors']) {
        $this->load->view('partials/header', $data);
        $this->load->view('dashboard/create-post');
        $this->load->view('partials/footer');
    } else {
        $post_image = 'default.jpg';
    }

} else {
    $data = array('upload_data' => $this->upload->data());
    $post_image = $_FILES['userfile']['name'];
}

Yet, I get a [i]Undefined variable: uerrors[/i] error.


Where is my mistake?
Reply
#2

(This post was last modified: 01-04-2020, 12:49 AM by snelledre.)

You don't load the data in the view.
PHP Code:
$this->load->view('dashboard/create-post'$data); 
See the tutorial in the documentation.
And if there are no errors?
What code have you in the view?

Andre
Reply
#3

(01-04-2020, 12:19 AM)snelledre Wrote: You don't load the data in the view.
PHP Code:
$this->load->view('dashboard/create-post'$data); 
See the tutorial in the documentation.
And if there are no errors?
What code have you in the view?

Andre

I do. Here is the entire method:

PHP Code:
public function create() {

    
// Only logged in users can create posts
    
if (!$this->session->userdata('is_logged_in')) {
        
redirect('login');
    }

    
$data $this->get_data();
    
$data['tagline'] = "Add New Post";

    if (
$data['categories']) {
        foreach (
$data['categories'] as &$category) {
            
$category->posts_count $this->Posts_model->count_posts_in_category($category->id);
        }
    }

    
$this->form_validation->set_rules('title''Title''required');
    
$this->form_validation->set_rules('desc''Short description''required');
    
$this->form_validation->set_rules('body''Body''required');
    
$this->form_validation->set_error_delimiters('<p class="error-message">''</p>');

    if(
$this->form_validation->run() === FALSE){
        
$this->load->view('partials/header'$data);
        
$this->load->view('dashboard/create-post');
        
$this->load->view('partials/footer');
    } else {
        
// Create slug (from title)
        
$slug url_title(convert_accented_characters($this->input->post('title')), 'dash'TRUE);
        
$slugcount $this->Posts_model->slug_count($slugnull);
        if (
$slugcount 0) {
            
$slug $slug."-".$slugcount;
        }

        
// Upload image
        
$config['upload_path'] = './assets/img/posts';
        
$config['allowed_types'] = 'jpg|jpeg|png';
        
$config['max_size'] = '2048';

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

        if(!
$this->upload->do_upload()){

        
    $data['uerrors'] = $this->upload->display_errors();

        
    if ($data['uerrors']) {
        
        $this->load->view('partials/header'$data);
        
        $this->load->view('dashboard/create-post');
        
        $this->load->view('partials/footer');
        
    } else {
        
        $post_image 'default.jpg';
        
    }

        } else {
        
    $data = array('upload_data' => $this->upload->data());
        
    $post_image $_FILES['userfile']['name'];
        }

        
$this->Posts_model->create_post($post_image$slug);
        
$this->session->set_flashdata('post_created''Your post has been created');
        
redirect('/');
    }

Reply
#4

Best,

What i mean is when you have no errors you must past "uerrors" also to you're view but then empty "".
But i can't see that because i can't see the full code off you're view and controller.
What i do is send the error with flash data.
for example.

PHP Code:
$config['upload_path']         = './assets/images/users/';
  
$config['allowed_types']       = 'jpg|jpeg|png';
  
$config['max_size']            = 500;
  
$config['overwrite']           = TRUE;
  
$config['remove_spaces']       = TRUE;
  
$config['encrypt_name']        = TRUE;

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

  if( ! 
$this->upload->do_upload($field_name))
  {
      
$data['error'] = $this->upload->display_errors();
      
$this->session->set_flashdata('UpdateProfilePicError'$data['error']);
      
redirect('edit_profile_pic');
  } 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB