Codeigniter文件上载-不填充上载文件夹

时间:2019-03-10 10:21:41

标签: php codeigniter file-upload codeigniter-3

在codeigntier中使用file_upload时遇到问题。

我没有在CI根目录下的上载文件夹中输入任何内容 夹。

我是这方面的初学者,并且一直在关注教程。

以下是我的视图和控制器的代码

查看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('xxxxx@xxxxx.com', '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");



        }

    }

}

我已经在it esle语句的内侧和外侧放置了文件上传功能,但仍然没有任何内容可以填充到我的上载文件夹中。

我已竭尽全力遵循针对file_upload的CodeIgniter文档,并在线上跟踪了许多教程。

但是仍然没有喜悦。

非常感谢您的帮助。

非常感谢, 标记

1 个答案:

答案 0 :(得分:1)

似乎您使用的是常规格式form_open。但是对于文件上传,您需要使用多部分表单打开。

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

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

阅读form_open_multipart