0

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

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>
4

1 回答 1

2

我将以这种方式解决这个问题:

$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
}

这是上传控制器的短代码。

于 2013-02-08T22:20:26.100 回答