-
eleumas Member
  
-
Posts: 66
Threads: 30
Joined: Jun 2017
Reputation:
1
Hi!
When i post the name of my images in database is the original name and not the new name that i have selected in $config['file_name'].
The images are uploaded on server with the new name (and this is ok), but in database are posted the original name (and isn't ok).
Thanks for help me.
PHP Code: // // CONTROLLER //
public function add() { if (!empty($_FILES)) { $folder = $this->input->post('id_article'); $file_name = $folder."_".date('dmy').'_';
if (!is_dir('/media/images/uploads/gallery/'.$folder)) { mkdir('./media/images/uploads/gallery/' . $folder, 0777, TRUE); }
$files = $_FILES; $count = count($_FILES['multimages']['name']);
for($i = 0; $i < $count; $i++) { // configurazione $config['upload_path'] = './media/images/uploads/gallery/'.$folder; $config['file_name'] = $file_name; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = 1024; $config['max_width'] = 1920; $config['max_height'] = 1080; $config['overwrite'] = FALSE; $config['remove_spaces'] = TRUE;
$_FILES['multimages']['name'] = $files['multimages']['name'][$i]; $_FILES['multimages']['type'] = $files['multimages']['type'][$i]; $_FILES['multimages']['tmp_name'] = $files['multimages']['tmp_name'][$i]; $_FILES['multimages']['error'] = $files['multimages']['error'][$i]; $_FILES['multimages']['size'] = $files['multimages']['size'][$i];
$this->load->library('upload', $config); // carico la libreria di CI $this->upload->initialize($config); // inizializzo la configurazione
if ($this->upload->do_upload('multimages')) { $data = array('upload_data' => $this->upload->data()); } else { $this->session->set_flashdata('result', '<div class="uk-alert-warning" uk-alert><a class="uk-alert-close" uk-close></a>Si è verificato un problema nel caricamento delle immagini!</div>'); redirect('images/index'); }
}
$name_image = $files['multimages']['name']; $data = array();
for($i=0; $i<$count; $i++) { $data[$i] = array( 'id_article' => $this->input->post('id_article'), 'name_image' => $name_image[$i], 'title_image' => $this->input->post('title_image')[$i], 'text_image' => $this->input->post('text_image')[$i], 'alt_image' => $this->input->post('alt_image')[$i], );
} $this->Images_model->add_image('tbl_images', $data);
$this->session->set_flashdata('result', '<div class="uk-alert-success" uk-alert><a class="uk-alert-close" uk-close></a>Immagini caricate correttamente!</div>'); redirect('images/index'); }
}
// // MODEL //
function add_image($table, $data) { $query = $this->db->insert_batch($table, $data); }
-
KoRsar4eg Newbie

-
Posts: 4
Threads: 1
Joined: Feb 2019
Reputation:
0
(02-09-2019, 05:29 AM)eleumas Wrote: Hi!
When i post the name of my images in database is the original name and not the new name that i have selected in $config['file_name'].
The images are uploaded on server with the new name (and this is ok), but in database are posted the original name (and isn't ok).
Thanks for help me.
PHP Code: // // CONTROLLER //
public function add() { if (!empty($_FILES)) { $folder = $this->input->post('id_article'); $file_name = $folder."_".date('dmy').'_';
if (!is_dir('/media/images/uploads/gallery/'.$folder)) { mkdir('./media/images/uploads/gallery/' . $folder, 0777, TRUE); }
$files = $_FILES; $count = count($_FILES['multimages']['name']);
for($i = 0; $i < $count; $i++) { // configurazione $config['upload_path'] = './media/images/uploads/gallery/'.$folder; $config['file_name'] = $file_name; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = 1024; $config['max_width'] = 1920; $config['max_height'] = 1080; $config['overwrite'] = FALSE; $config['remove_spaces'] = TRUE;
$_FILES['multimages']['name'] = $files['multimages']['name'][$i]; $_FILES['multimages']['type'] = $files['multimages']['type'][$i]; $_FILES['multimages']['tmp_name'] = $files['multimages']['tmp_name'][$i]; $_FILES['multimages']['error'] = $files['multimages']['error'][$i]; $_FILES['multimages']['size'] = $files['multimages']['size'][$i];
$this->load->library('upload', $config); // carico la libreria di CI $this->upload->initialize($config); // inizializzo la configurazione
if ($this->upload->do_upload('multimages')) { $data = array('upload_data' => $this->upload->data()); } else { $this->session->set_flashdata('result', '<div class="uk-alert-warning" uk-alert><a class="uk-alert-close" uk-close></a>Si è verificato un problema nel caricamento delle immagini!</div>'); redirect('images/index'); }
}
$name_image = $files['multimages']['name']; $data = array();
for($i=0; $i<$count; $i++) { $data[$i] = array( 'id_article' => $this->input->post('id_article'), 'name_image' => $name_image[$i], 'title_image' => $this->input->post('title_image')[$i], 'text_image' => $this->input->post('text_image')[$i], 'alt_image' => $this->input->post('alt_image')[$i], );
} $this->Images_model->add_image('tbl_images', $data);
$this->session->set_flashdata('result', '<div class="uk-alert-success" uk-alert><a class="uk-alert-close" uk-close></a>Immagini caricate correttamente!</div>'); redirect('images/index'); }
}
// // MODEL //
function add_image($table, $data) { $query = $this->db->insert_batch($table, $data); }
I think you set file names for $_FILES array, which actually has your original files name. Take new file name from upload->data() and write it to another array, then take names from it. For instance like this:
PHP Code: if (!empty($_FILES)) { $names = array(); ... if ($this->upload->do_upload('multimages')) [size=small][font=Monaco, Consolas, Courier, monospace] { [/font][/size] [b] $uploadData = [size=small][font=Monaco, Consolas, Courier, monospace]$this->upload->data();[/font][/size][/b] [size=small][font=Monaco, Consolas, Courier, monospace] $data = array('upload_data' => $[b]uploadData[/b]); [/font][/size] [b]$names[] = $uplaodData['file_name'];[/b] ... }
...
for($i=0; $i<$count; $i++) [size=small][font=Monaco, Consolas, Courier, monospace] {[/font][/size] [size=small][font=Monaco, Consolas, Courier, monospace] $data[$i] = array([/font][/size] [size=small][font=Monaco, Consolas, Courier, monospace] 'id_article' => $this->input->post('id_article'),[/font][/size] [size=small][font=Monaco, Consolas, Courier, monospace] 'name_image' => $[b]names[/b][$i],[/font][/size] [size=small][font=Monaco, Consolas, Courier, monospace] 'title_image' => $this->input->post('title_image')[$i],[/font][/size] [size=small][font=Monaco, Consolas, Courier, monospace] 'text_image' => $this->input->post('text_image')[$i],[/font][/size] [size=small][font=Monaco, Consolas, Courier, monospace] 'alt_image' => $this->input->post('alt_image')[$i],[/font][/size] [size=small][font=Monaco, Consolas, Courier, monospace] );[/font][/size]
}
|