Thursday, 15 March 2012

php - Navigate to a $_FILE array to get particular array of files with different name[] -



php - Navigate to a $_FILE array to get particular array of files with different name[] -

overview

i have multi-part form has 4 file upload fields , 3 of these dynamically added (using javascript).

<input type="file" name="onecertificate" /> <input type="file" id="multiplecertificate[1] "name="multiplecertificate[]" /> <input type="file" id="multiplecertificate[2] "name="multiplecertificate[]" /> // more if add together button pressed

here output of var_dump($_files):

["onecertificate"]=> array(5) { ["name"]=> string(6) "de.pdf" ["type"]=> string(15) "application/pdf" ["tmp_name"]=> string(24) "c:\xampp\tmp\php37c2.tmp" ["error"]=> int(0) ["size"]=> int(103845) } // **notice attributes in own arrays** ["multiplecertificate"]=> array(5) { ["name"]=> array(2) { [0]=> string(6) "de.pdf" [1]=> string(6) "de.pdf" } ["type"]=> array(2) { [0]=> string(15) "application/pdf" [1]=> string(15) "application/pdf" } ["tmp_name"]=> array(2) { [0]=> string(24) "c:\xampp\tmp\phpd941.tmp" [1]=> string(24) "c:\xampp\tmp\phpd942.tmp" } ["error"]=> array(2) { [0]=> int(0) [1]=> int(0) } ["size"]=> array(2) { [0]=> int(103845) [1]=> int(103845) } } // , on...

below how upload each file:

function upload_file($field_name) { // timestamp name: http://stackoverflow.com/questions/7457152/did-not-select-a-file-to-upload-when-uploading-using-codeigniter $the_date= date('y/n/j h:i:s'); $replace = array(":"," ","/"); $new_name = str_ireplace($replace, "-", $the_date); $config['upload_path'] = './uploads/'; $config['file_name'] = $new_name; $config['allowed_types'] = 'pdf|jpg|jpeg|png'; $this->load->library('upload'); $this->upload->initialize($config); // onecertificate normal way since have 1 file content if ( $field_name == 'onecertificate' ) { if ( ! $this->upload->do_upload($field_name)) { homecoming array('error' => $this->upload->display_errors()); } else { $file_data = array('upload_data' => $this->upload->data()); } // method multiplecertificate } else { ($i = 0; $i < count($_files[$field_name]['name']); $i++) { if ( ! $this->upload->do_upload($_files[$field_name]['name'][$i])) { $file_data = array('error' => $this->upload->display_errors()); } else { $file_data = array('upload_data' => $this->upload->data()); } } // end loop } homecoming $file_data; } the problem

i noticed format of onecertificate works since all of info in single array compared multiplecertificate has each attribute in own array.

the first manages upload file latter throws you did not select file upload.

how transform and/or retrieve form of multiplecertificate onecertificate?

note: form need since assign arrays created $onecertificate , $multiplecertificate database insertion.

for had problem mine regarding how $_files handles file arrays, hohner's comment above solved problem.

here link answer.

basically "looping inner arrays of $_file array" goes this:

function do_upload($file_name) { $this->load->library('upload'); $this->total_count_of_files = count($_files[$file_name]['name']); // timestamp name: http://stackoverflow.com/questions/7457152/did-not-select-a-file-to-upload-when-uploading-using-codeigniter $date_timestamp = date('y/m/j h:i:s'); $replace = array(":"," ","/"); $new_name = $file_name.'-'.$this->session->userdata('accreditno').'-'.str_ireplace($replace, '', $date_timestamp); for($i = 0; $i < $this->total_count_of_files; $i++) { // userfile arbitrary, not reflect on final array $_files['userfile']['name'] = $_files[$file_name]['name'][$i]; $_files['userfile']['type'] = $_files[$file_name]['type'][$i]; $_files['userfile']['tmp_name'] = $_files[$file_name]['tmp_name'][$i]; $_files['userfile']['error'] = $_files[$file_name]['error'][$i]; $_files['userfile']['size'] = $_files[$file_name]['size'][$i]; $config['file_name'] = $new_name.'-'.$i; $config['upload_path'] = "./uploads/$file_name"; $config['allowed_types'] = 'pdf|jpg|jpeg|png'; $config['max_size'] = '0'; $this->upload->initialize($config); if ( ! $this->upload->do_upload()) { $file_data = array('error' => $this->upload->display_errors()); } else { $file_data = array("file_{$i}" => $this->upload->data()); } } homecoming $file_data; }

file info contain assembled attributes of 1 file inserted database! (how want in case)

php arrays codeigniter file-io

No comments:

Post a Comment