实际上,我正在尝试在codigniter中上传文件,但已经完成,但是我想如果文件在上传文件夹中已经存在,则会显示错误消息,请帮帮我。
这是我的代码控制器:
if($this->input->post('userSubmit'))
{
//echo "comm";
if(!empty($_FILES['file_upload']['name']))
{
// echo "coming";
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'doc|docx|pdf';
$config['file_name'] = $_FILES['file_upload']['name'];
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('file_upload'))
{
//echo "cominggggggg";die;
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
//print_r($picture);die;
}
else
{
$picture = '';
}
}
else
{
$picture = '';
}
}
答案 0 :(得分:0)
您不需要初始化CI上传设置两次,而应使用./uploads/
而不是uploads/
。否则,只需检查即可:
if ($this->input->post('userSubmit')) {
if (!empty($_FILES['file_upload']['name'])) {
if (is_file('./uploads/' . $_FILES['file_upload']['name'])) {
show_error('File already exists!'); // function exits
}
$config['upload_path'] = './uploads/'; // correct way
$config['allowed_types'] = 'doc|docx|pdf';
$config['file_name'] = $_FILES['file_upload']['name'];
$this->load->library('upload', $config);
//$this->upload->initialize($config); you already did this ^
if ($this->upload->do_upload('file_upload')) {
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
} else {
$picture = '';
}
} else {
$picture = '';
}
}