CodeIgniter Forums
post the new name of images in database with insert batch - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: post the new name of images in database with insert batch (/showthread.php?tid=72771)



post the new name of images in database with insert batch - eleumas - 02-09-2019

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/' $folder0777TRUE);
 
   }

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



RE: post the new name of images in database with insert batch - KoRsar4eg - 02-14-2019

(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/' $folder0777TRUE);
 
   }

 
   $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=MonacoConsolasCouriermonospace     {
[/
font][/size]       [b$uploadData = [size=small][font=MonacoConsolasCouriermonospace]$this->upload->data();[/font][/size][/b]
[
size=small][font=MonacoConsolasCouriermonospace       $data = array('upload_data' => $[b]uploadData[/b]);
[/
font][/size]        [b]$names[] = $uplaodData['file_name'];[/b]
...
 
     }

...


for(
$i=0$i<$count$i++)
[
size=small][font=MonacoConsolasCouriermonospace   {[/font][/size]
[
size=small][font=MonacoConsolasCouriermonospace     $data[$i] = array([/font][/size]
[
size=small][font=MonacoConsolasCouriermonospace       'id_article'  => $this->input->post('id_article'),[/font][/size]
[
size=small][font=MonacoConsolasCouriermonospace       'name_image'  => $[b]names[/b][$i],[/font][/size]
[
size=small][font=MonacoConsolasCouriermonospace       'title_image' => $this->input->post('title_image')[$i],[/font][/size]
[
size=small][font=MonacoConsolasCouriermonospace       'text_image'  => $this->input->post('text_image')[$i],[/font][/size]
[
size=small][font=MonacoConsolasCouriermonospace       'alt_image'   => $this->input->post('alt_image')[$i],[/font][/size]
[
size=small][font=MonacoConsolasCouriermonospace     );[/font][/size]

 
   



RE: post the new name of images in database with insert batch - eleumas - 02-20-2019

Thanks for your answer! Now work Smile