【发布时间】:2021-02-15 04:11:37
【问题描述】:
我在 codeigniter 中上传了多个图像,但是当我使用此代码设置新名称时 $_FILES['attachment']['name']= $filename;
我明白了
您尝试上传的文件类型不允许。
请提出解决此问题的解决方案。
public function do_upload()
{
if (($_SERVER['REQUEST_METHOD']) == "POST") {
$count = count($_FILES['attach_file']['name']);
$files = $_FILES;
for($i=0; $i<$count; $i++){
$filename = $_FILES['attach_file']['name'][$i];
$filename = strstr($filename, '.', true);
$email = $this->session->userdata('email');
$filename = strstr($email, '@', true)."_".$filename;
$filename = strtolower($filename);
$_FILES['attachment']['name']= $filename;
$_FILES['attachment']['type']= $_FILES['attach_file']['type'][$i];
$_FILES['attachment']['tmp_name']= $_FILES['attach_file']['tmp_name'][$i];
$_FILES['attachment']['error']= $_FILES['attach_file']['error'][$i];
$_FILES['attachment']['size']= $_FILES['attach_file']['size'][$i];
$config['upload_path'] = FCPATH .'./assets/attachments/new/';
$config['allowed_types'] = 'pdf|doc|docx|bmp|gif|jpg|jpeg|jpe|png';
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['encrypt_name'] = true;
$config['file_ext_tolower'] = true;
$config['overwrite'] = false;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('attachment')) {
$data['exception'] = $this->upload->display_errors();
$data['status'] = false;
echo json_encode($data);
} else {
$upload = $this->upload->data();
$data['message'] = 'upload_successfully';
$data['filepath'] = './assets/attachments/'.$upload['file_name'];
$data['status'] = true;
echo json_encode($data);
}
}
}
}
【问题讨论】:
-
您上传的是哪种文件类型??
-
@KUMAR:我正在上传 jpeg 类型的文件
-
请删除
$config['max_size'] = 0;$config['max_width'] = 0;$config['max_height'] = 0;并告诉我。 -
您使用的是哪个版本:CI 2.x 还是 3.x?
-
@KUMAR: 删除代码但得到相同的错误
标签: image codeigniter model-view-controller codeigniter-3 codeigniter-2