0

I'm uploading multiple image in codeigniter, But when i set new name using this code $_FILES['attachment']['name']= $filename;

I'm getting

The filetype you are attempting to upload is not allowed.

Please suggest solution to solve this issue.

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
  • 1,993
  • 2
  • 9
  • 26
user3653474
  • 3,393
  • 6
  • 49
  • 135

1 Answers1

0

Note:- You are assign same variable name again & again $filename

Please Change this:-

$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;

to :-

$filename = $_FILES['attach_file']['name'][$i];
$filename1 = strstr($filename, '.', true);
$email    = $this->session->userdata('email');
$filename2 = strstr($email, '@', true)."_".$filename1;
$filename3 = strtolower($filename2);  
KUMAR
  • 1,993
  • 2
  • 9
  • 26