Php Codeigniter使图像上传在表单中可选
我有一个有三个字段的表单——一个输入字段、一个文本区域和一个图像上传。 当我填写所有内容并选择要上传的图像时,该表单可以很好地上传图像,但当我没有选择图像时,它会显示select and image。我希望表单提交,即使用户没有上传图像。我相信我昨天已经找到了解决办法,但今天它停止了工作。 控制器:Php Codeigniter使图像上传在表单中可选,php,codeigniter,Php,Codeigniter,我有一个有三个字段的表单——一个输入字段、一个文本区域和一个图像上传。 当我填写所有内容并选择要上传的图像时,该表单可以很好地上传图像,但当我没有选择图像时,它会显示select and image。我希望表单提交,即使用户没有上传图像。我相信我昨天已经找到了解决办法,但今天它停止了工作。 控制器: function store() { $this->output->enable_profiler(TRUE); $this->load->
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');
}
}
}
}
我以前确实尝试过,但它为图像上传提供了一个错误,没有上传图像,而是将默认文件的名称保存到数据库中。它执行“else”语句,但每次都执行包含“empty”子句的“if”块。数组([name]=>images(1).jpg[type]=>image/jpeg[tmp\u name]=>C:\xampp\tmp\php3EC6.tmp[error]=>0[size]=>9852)答案是$\u文件不是空的,如果你在其中没有任何内容的情况下使用var\u dump($\u文件),它将返回一个带有一些值的数组,使空($\u文件)返回false,您必须首先测试它是否为
isset
,然后您将知道已提交了文件上载,然后您将检测该文件是否有名称,查看您刚才测试整个文件的代码,我确实尝试了if(空($\u FILES['userfile']['userfile']),但仍然使用包含“userfile”的名称也是文件名。php中没有名为userfile的文件属性,只有名称
,大小
,类型
,tmp_名称
,错误
。查看手册,您必须将其用作$\u文件['userfile']['name']
@tomexans文件上载的输入如下:
if(isset($_FILES))
{
if(empty($_FILES['file']['name']))
{
//logic here
}else{
if(//validation here )
{
//logic here
}
}
}