我想验证文件上传,条件是文件输入可以上传文件,如果文件输入为空,但如果文件输入与文件设置的类型不匹配则显示错误。如何制作这些错误。如果我使用这个脚本:!$this->upload->do_upload('file')
并且我让文件输入为空,肯定会出现错误。
这是我的配置上传:
$config['upload_path'] = '../../upload/file';
$config['allowed_types'] = 'pdf';
$config['file_name'] = 'file_'.time();
$config['overwrite'] = true;
$this->upload->initialize($config);
if(!$this->upload->do_upload('file')){
$respond['error'] = true;
$respond['message'] = "Error Upload".$this->upload->display_errors();
}
答案 0 :(得分:0)
您可以添加条件,因此只会验证是否有上传的文件:
<?php
if ( $_FILES && $_FILES['file']['name'] )
{
$config['upload_path'] = '../../upload/file';
$config['allowed_types'] = 'pdf';
$config['file_name'] = 'file_'.time();
$config['overwrite'] = true;
$this->upload->initialize($config);
if(!$this->upload->do_upload('file')){
$respond['error'] = true;
$respond['message'] = "Error Upload".$this->upload->display_errors();
}
}
答案 1 :(得分:0)
此解决方案将非常适合您
$config['upload_path'] = './uploads/;
$config['allowed_types'] = 'pdf';
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['file_name'] = round(100,99); //make your file name random
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('file_name')){
print $this->upload->data();
}else{
print $this->upload->display_errors();
}