Reputation: 51
i'm try to build REST API use codeigniter plugin from https://github.com/chriskacerguis/codeigniter-restserver
i'm successfull built multiple upload with this, but i have a trouble when upload many file data. when i'm select 3 file in my directory, my code work, but in upload path i just have 2 file.
here controller :
function upload_post()
{
$name_array = array();
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key=>$value)
{
for($s=0; $s<=$count-1; ) {
$_FILES['userfile']['name']=$value['name'][$s];
$_FILES['userfile']['type'] = $value['type'][$s];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['userfile']['error'] = $value['error'][$s];
$_FILES['userfile']['size'] = $value['size'][$s];
$config['upload_path'] = 'E:/tes/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
$name_array[] = $data['file_name'];
$s++;
}
}
$names= implode(',', $name_array);
/* $this->load->database();
$db_data = array('id'=> NULL,
'name'=> $names);
$this->db->insert('testtable',$db_data);
*/ print_r($names);
print_r($count);
}
Upvotes: 2
Views: 1896
Reputation: 7134
I have made a helper function for me which upload the files and return a array with upload result.It takes a parameter upload directory where to save the files. Here is the my function
function upload_file($save_dir="images")//takes folder name where to save.default images folder
{
$CI = & get_instance();
$CI->load->library('upload');
$config=array();
$config['upload_path'] = FCPATH.$save_dir;
$config['allowed_types'] = 'gif|jpg|png';//only images.you can set more
$config['max_size'] = 1024*10;//10 mb you can increase more.Make sure your php.ini has congifured same or more value like this
$config['overwrite'] = false;//if file do not replace.create new file
$config['remove_spaces'] = true;//remove sapces from file
//you can set more config like height width for images
$uploaded_files=array();
foreach ($_FILES as $key => $value)
{
if(strlen($value['name'])>0)
{
$CI->upload->initialize($config);
if (!$CI->upload->do_upload($key))
{
$uploaded_files[$key]=array("status"=>false,"message"=>$value['name'].': '.$CI->upload->display_errors());
}
else
{
$uploaded_files[$key]=array("status"=>true,"info"=>$CI->upload->data());
}
}
}
return $uploaded_files;
}
Sample output
if you print out the function returns you will get the output like this
Array
(
[file_input_name1] => Array
(
[status] => 1//status will be true if uploaded
[info] => Array //if success it will return the file informattion
(
[file_name] => newfilename.jpg
[file_type] => image/jpeg
[file_path] => C:/Program Files (x86)/VertrigoServ/www/rnd/images/
[full_path] => C:/Program Files (x86)/VertrigoServ/www/rnd/images/newfilename.jpg
[raw_name] => newfilename
[orig_name] => newfilename.jpg
[client_name] => newfilename
[file_ext] => .jpg
[file_size] => 762.53
[is_image] => 1
[image_width] => 1024
[image_height] => 768
[image_type] => jpeg
[image_size_str] => width="1024" height="768"
)
)
[file_input_name_2] => Array
(
[status] =>//if invalid file status will be false with error message
[message] => desktop.ini: <p>The filetype you are attempting to upload is not allowed.</p>
)
)
Hope it may help you
Upvotes: 1