0

I am having issues using the file_upload in codeigntier.

I am not getting any input into my uploads folder in the root of my CI folder.

I am a beginner at this and have just been following tutorials.

The following is the code for my view and controller

View create_ticket.php

<h2>Create Ticket</h2>
<?php $attributes = array('id'=>'create_form', 'class'=> 'form_horizontal'); ?>

<?php echo validation_errors("<p class='bg-danger'>"); ?>
                        <!-- //controller -->
<?php echo form_open('tickets/create', $attributes);?>  

<div class="form-group">
<?php      
    echo form_label('Title: ');
    $data = array(  

        'class' => 'form-control',
        'name' => 'ticket_title',
        'placeholder' => 'e.g Microsoft Outlook Running Slow',
        'value' => set_value('ticket_title')

    );
    echo form_input($data); 
?>
</div>
<!-- //break -->
<div class="form-group">
<?php      
    echo form_label('Description of Fault: ');
    $data = array(  

        'class' => 'form-control',
        'name' => 'ticket_description',
        'placeholder' => 'Desribe the Fault',
        'value' => set_value('ticket_description')

    );
    echo form_textarea($data);



?>
</div>
<div class="form-group">
          <?php echo form_label('Upload:'); ?>
          <?php
          $data = array(
            'class' => 'input-file',
            'name' => 'file_upload',
            'accept' => 'pdf,jpg,png',
            'type' => 'file' ); ?>
          <?php echo form_input($data); ?>
        </div>


<div class="form-group">
<?php

    $data = array(

        'class' => 'btn btn-primary',
        'name' => 'submit',
        'value' => 'Report Ticket'

        );


     ?>



    <?php echo form_submit($data); ?>



    </div>
    <?php

?>

<?php echo form_close(); ?>

Controller tickets.php

public function create(){
        //validation
        $this->form_validation->set_rules('ticket_title', 'ticket title', 'trim|required|min_length[1]');
        $this->form_validation->set_rules('ticket_description', 'ticket description', 'trim|required|min_length[3]');

    if($this->form_validation->run() == FALSE){

        $data['main_view'] = 'tickets/create_ticket';
        $this->load->view('layouts/main', $data);

    }else{


        $data = array(

            'ticket_creator_user_id' =>$this->session->userdata('user_id'),
            'ticket_creator' =>$this->session->userdata('username'),
            'ticket_title' =>$this->input->post('ticket_title'),
            'ticket_description' =>$this->input->post('ticket_description')

        ); 


        if($this->ticket_model->create_ticket($data)){


            //email user to say ticket has been created

        $user = $this->session->userdata('username');
        $email = $this->session->userdata('email');


        $ticket_title = $this->input->post('ticket_title');
        $ticket_id = $this->input->post('ticket_id');
        // $email = $this->user_model->fetch_email();
        $this->load->library('email');
        $this->email->set_newline("\r\n");

        $this->email->from('[email protected]', 'xxxxxx xxxxxx');
        $this->email->to($email);
        $this->email->subject('Helpdesk Account');
        $this->email->message('Hey ');


        $path = $this->config->item('server_root');
        $file = $path . '/helpdesk/attachments/yourInfo.txt';
        $this->email->attach($file);

        if($this->email->send())
        {
            echo 'Your Email was sent';
        }else
        {

            show_error($this->email->print_debuger());


            $config['upload_path']          = './uploads/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 100;
            $config['max_width']            = 1024;
            $config['max_height']           = 768;

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

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

                   // $this->load->view('upload_form', $error);
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());

                   // $this->load->view('upload_success', $data);
            }    

        }
            redirect("tickets/index");



        }

    }

}

I have placed the file upload function in and out side of the it esle statement and there is still nothing populating through to my uploads folder.

I have followed to the best of my ability the CodeIgniter documentation for file_upload and followed numerous tutorials online.

However still no joy.

Any help is greatly appreciated.

Many Thanks, Mark

1 Answer 1

1

Seems that you are using normal form form_open. But for file upload you need to use multipart form open.

Replace <?php echo form_open('tickets/create', $attributes);?>

with <?php echo form_open_multipart('tickets/create', $attributes);?>

Read form_open_multipart

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

1 Comment

Thanks Saji, That fixed it. Appreciate the help!

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.