Welcome Guest, Not a member yet? Register   Sign In
multiple file upload method
#1

[eluser]Krystian[/eluser]
Hi,
view
Code:
echo form_open_multipart(base_url() . 'media');
      
        echo form_upload(array('name'=>'pro_image[]', 'type'=>'file', 'multiple'=>'multiple', 'accept'=>'image/*'));
      
        echo form_submit('value','OK');
        
       echo form_close();

contoller
Code:
$this->load->model('file_model');
  
  //var_dump($_FILES);
      
  foreach( $_FILES['pro_image']['name'] as $key => $image_name )
  {
   echo $image_name . '<br />';
   $this->file_model->make_upload($image_name, 'jpg|gif|png|jpeg', 1);
  }

and model method
Code:
class File_model extends CI_Model {

//upload files
function make_upload($my_file, $types, $resize)
{
  
  $this->load->library('upload');
  $config = array(
   'allowed_types' => $types,  //'jpg|jpeg|gif|png',
   'upload_path' => './img/uploads/media_library', //$this->gallery_path,
   'overwrite' => true,
   'max_size' => 4000
  );
  
  $this->upload->initialize($config);

  $this->upload->do_upload($my_file);
  $image_data = $this->upload->data();
  
  if($resize == 1)
  {
   $config = array(
    'source_image' => $image_data['full_path'],
    'new_image' => './img/uploads/media_library/thumbs',
    //'maintain_ratio' => true,
    'width' => 90,
    'height' => 90
   );
  
   $this->load->library('image_lib', $config);
   $this->image_lib->resize();
  
  }
  
  //return $image_data['file_name'];  
}

this code do nothing wrrr.
And the thing is that I cannot see what is wrong with it. Can you give me a tip?

EDIT: when i change to this in file_model
Code:
if(! $this->upload->do_upload($my_file))
  {
   echo $this->upload->display_errors();
  }
CI gave me a message "You did not select a file to upload"
#2

[eluser]LuckyFella73[/eluser]
When calling the do_upload method your input type file formfield
need to have the name attribute set to "userfile" or you have to
define the other value like this:

Code:
$field_name = "some_field_name"; // in your case it would be : "pro_image"
$this->upload->do_upload($field_name)

I would focus on that.

It's not tested but you can try this:
Code:
$field_name = "pro_image";
foreach( $_FILES[$field_name]['name'])
{
$this->file_model->make_upload($field_name, 'jpg|gif|png|jpeg', 1);
}

function make_upload($field_name = 'userfile' /*standard value*/, $types, $resize)
{
$this->load->library('upload');
$config = array(
  'allowed_types' => $types,  //'jpg|jpeg|gif|png',
  'upload_path' => './img/uploads/media_library', //$this->gallery_path,
  'overwrite' => true,
  'max_size' => 4000
);

$this->upload->initialize($config);

$this->upload->do_upload($field_name);
$image_data = $this->upload->data();

// ...
#3

[eluser]Krystian[/eluser]
yes I know that do_upload need to have field name ( in my code is file name which is wrong )
but in this case my field name is an array ( much simpler is when the field is single )

If have some time can you please test my code on your CI installation?
Now I don`t know how to write this loop, because the make_upload() method is for sure good. It`s working when I`ve tried on single input ( not multiple )




Theme © iAndrew 2016 - Forum software by © MyBB