Php Codeigniter-具有可选文件上载字段的表单意外工作

Php Codeigniter-具有可选文件上载字段的表单意外工作,php,forms,codeigniter,Php,Forms,Codeigniter,我有一个表单,要求用户的名称和描述,以及一个可选的图像上传字段。当用户上传图像时,我希望更改图像的名称并将其存储到数据库中,如果没有上传图像,则默认名称存储在数据库中。如果用户上传了一个图像,它可以正常工作,但是如果用户没有上传,那么就不会将任何内容上传到数据库中。 这是控制器的功能: function store() { $this->load->model('campus_m'); $config['upload_path'] = './uploads/';

我有一个表单,要求用户的名称和描述,以及一个可选的图像上传字段。当用户上传图像时,我希望更改图像的名称并将其存储到数据库中,如果没有上传图像,则默认名称存储在数据库中。如果用户上传了一个图像,它可以正常工作,但是如果用户没有上传,那么就不会将任何内容上传到数据库中。 这是控制器的功能:

function store()
{
    $this->load->model('campus_m');
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    $config['file_name'] = preg_replace('/[^a-z0-9]+/i','-',iconv('UTF-8','ASCII//TRANSLIT',$this->input->post('name')));
    $config['file_name'] = trim($config['file_name'],'-').now().'.jpg';                         

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

    $this->load->helper(array('form', 'url'));

    $this->load->library('form_validation');

    $this->form_validation->set_rules('goods', 'Goods', 'required');
    $this->form_validation->set_rules('name', 'Name', 'required|max_length[12]');
    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('campus_write_v');
    }
    else
    {
        print_r($_FILES['userfile']);
        if (empty($_FILES['userfile'])) {
            if(!$query = $this->campus_m->create_review("Marla-overdoses1360186300.jpg")){
                $data['write_campus'] = 'The answer has not been stored.';
                $this->load->view('campus_write_v', $data);
            }
            else {
                $data['write_campus'] = 'The answer has been stored. ';
                $this->load->view('campus_write_v', $data);
            }   
        }
        else
        {
            if($this->upload->do_upload()){
                if(!$query = $this->campus_m->create_review($config['file_name'])){
                    $data['write_campus'] = 'The answer has not been stored.';
                    $this->load->view('campus_write_v', $data);
                }
                else {
                    $data['write_campus'] = 'The answer has been stored. ';
                    $this->load->view('campus_write_v', $data);
                }
            }
            else
            {
                $error = array('error' => $this->upload->display_errors());
                foreach ($error as $rows => $r)
                {
                    echo $r ;                 
                }
                $this->load->view('campus_write_v');                             
            }
        }
    }
}
这是一种观点:

<?php
                $attributes = array('id' => 'contactform');
                echo form_open_multipart('campus/store', $attributes);
                ?>
                <div>
                <?php
                echo form_label('Title of Area');
                $data = array(
                    'name'        => 'name',
                    'id'          => 'name',
                    'placeholder' => 'Location and name of the place',
                    'required' => 'required',
                    'value'       => set_value("name") 
                    );

                echo form_input($data);    
                ?>
                </div>

                <div>
                <?php
                    $data = array(
                    'name'        => 'goods',
                    'id'          => 'goods',
                    'placeholder' => 'Tell us about the place',
                    'required' => 'required',
                    'value'       => set_value("goods"),
                    'rows'   => '20',
                    'cols'        => '50'
                    );

                echo form_textarea($data);    
                ?>
                </div>
                <div>
            <input type="file" id="userfile" name="userfile" size="20" />
        </div>

                <div>
                    <?php
                    $data = array(
                    'class'        => 'button',
                    'value'       => 'Submit',
                    'id'          =>"submit"
                    );

                echo form_submit($data);
                    ?>

        </div>
                  <?php     echo form_close();
                    ?>
        </div>

我将通过以下方式解决此问题:

$config['upload_path'] = './uploads/'; 
$config['allowed_types'] = 'gif|jpg|png';

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

if($this->upload->do_upload('userfile')){
    $data = $this->upload->data();
    $photo['image'] = $data['file_name']; // Name of image
} else {
    $photo['image'] = "Name"; // Name that you want
}

这是uploads controller的简短代码。

我是否应该假定您没有收到消息
答案尚未存储。
这行代码对我有用<代码>$this->上传->初始化($config)