php - Trying to upload multiple files in codeignitor. Getting do_upload expects a sting array given error -
i trying upload multiple files in codeigniter. getting below warning , error
a php error encountered severity: warning message: is_uploaded_file() expects parameter 1 string, array given filename: libraries/upload.php error: did not select file upload.
here file upload form control:
<input type="file" accept="image/png, image/jpeg, image/gif, application/pdf, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, text/plain, application/pdf" name="file[]" multiple/>
my controller function here $file name of file being uploaded pass file_upload() other function.
public function file_upload($file){ $new_file = ""; $original_file_name = ''; if($file!=""){ $file_name = $file; $original_file_name = $file_name; $random = rand(1, 10000000000000000); $makerandom = hash('sha512', $random.$this->input->post('title') . config_item("encryption_key")); $file_name_rename = $makerandom; $explode = explode('.', $file_name); if(count($explode) >= 2) { $new_file = $file_name_rename.'.'.$explode[1]; $config['upload_path'] = "./uploads/images"; $config['allowed_types'] = "gif|jpg|png|jpeg|pdf|doc|xml|docx|gif|jpg|png|jpeg|pdf|doc|xml|docx|xls|xlsx|txt|ppt|csv"; $config['file_name'] = $new_file; $config['max_size'] = '3072'; $config['max_width'] = '3000'; $config['max_height'] = '3000'; $this->load->library('upload',$config); $this->upload->initialize($config); if(!$this->upload->do_upload("file")) { print_r($this->upload->display_errors()); } else { echo "success"; } } else { //error } }else{ //some code here } }
imho code doesn't make sense - main problem here - you've change _files
array
something should work
public function file_upload() { $strinputfilename = "file"; $arrfiles = $_files; $config['upload_path'] = "./uploads/images"; $config['allowed_types'] = "gif|jpg|png|jpeg|pdf|doc|xml|docx|gif|jpg|png|jpeg|pdf|doc|xml|docx|xls|xlsx|txt|ppt|csv"; $config['file_name'] = $new_file; $config['max_size'] = '3072'; $config['max_width'] = '3000'; $config['max_height'] = '3000'; $config['file_name'] = $this->getrandomfilename(); if (is_array($_files[$strinputfilename]['name'])) { $countfiles = count($_files[$strinputfilename]['name']); for($i=0;$i<$countfiles; $i++) { //overwrite _files array $_files[$strinputfilename]['name'] = $arrfiles[$strinputfilename]['name'][$i]; $_files[$strinputfilename]['type'] = $arrfiles[$strinputfilename]['type'][$i]; $_files[$strinputfilename]['tmp_name'] = $arrfiles$strinputfilename]['tmp_name'][$i]; $_files[$strinputfilename]['error'] = $arrfiles[$strinputfilename]['error'][$i]; $_files[$strinputfilename]['size'] = $arrfiles[$strinputfilename]['size'][$i]; $this->upload->initialize($config); if(!$this->upload->do_upload($strinputfilename)) { print_r($this->upload->display_errors()); } else { echo "success"; } } } else { $this->upload->initialize($config); if(!$this->upload->do_upload($strinputfilename)) { print_r($this->upload->display_errors()); } else { echo "success"; } } } private function getrandomfilename() { $random = rand(1, 10000000000000000); return hash('sha512', $random.$this->input->post('title') . config_item("encryption_key")); }
Comments
Post a Comment