【发布时间】:2013-02-07 23:39:35
【问题描述】:
我有一个包含三个字段的表单——一个输入字段、一个文本区域和一个图像上传。 当我填写所有内容并选择要上传的图像时,该表单可以正常上传图像,但是当我不选择图像时,它会显示选择和图像。即使用户不上传图片,我也希望提交表单。我确信我昨天有一个解决方案,但它今天停止工作。 控制器:
function store()
{
$this->output->enable_profiler(TRUE);
$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
{
if (empty($_FILES['userfile'])) {
print_r($_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 codeigniter