I've been searching and struggling for 3 days now to make this works but I just can't. What I want to do is use a Multiple file input form and then upload them. I can't just use a fixed number of file to upload. I tried many many solutions on StackOverflow but I wasn't able to find a working one.
Here's my Upload controller
<?php class Upload extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper(array('form', 'url','html')); } function index() { $this->load->view('pages/uploadform', array('error' => ' ' )); } function do_upload() { $config['upload_path'] = './Images/'; $config['allowed_types'] = 'gif|jpg|png'; $this->load->library('upload'); foreach($_FILES['userfile'] as $key => $value) { if( ! empty($key['name'])) { $this->upload->initialize($config); if ( ! $this->upload->do_upload($key)) { $error['error'] = $this->upload->display_errors(); $this->load->view('pages/uploadform', $error); } else { $data[$key] = array('upload_data' => $this->upload->data()); $this->load->view('pages/uploadsuccess', $data[$key]); } } } } } ?>
My upload form is This.
<html> <head> <title>Upload Form</title> </head> <body> <?php echo $error;?> <?php echo form_open_multipart('upload/do_upload');?> <input type="file" multiple name="userfile[]" size="20" /> <br /><br /> <input type="submit" value="upload" /> </form> </body> </html>
I just keep having this error :
You did not select a file to upload.
Here's the array of the example:
Array ( [userfile] => Array ( [name] => Array ( [0] => youtube.png [1] => zergling.jpg ) [type] => Array ( [0] => image/png [1] => image/jpeg ) [tmp_name] => Array ( [0] => E:\wamp\tmp\php7AC2.tmp [1] => E:\wamp\tmp\php7AC3.tmp ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 35266 [1] => 186448 ) ) )
I have this like 5 times in a row if I select 2 files. I also use the standard Upload library.
You can just use one file upload button, and add multiple="multiple" attribute in the button and differentiate between the file type on server side. You can take a look at this answer and upload based on the file type.
I finally managed to make it work with your help!
Here's my code:
function do_upload() { $this->load->library('upload'); $files = $_FILES; $cpt = count($_FILES['userfile']['name']); for($i=0; $i<$cpt; $i++) { $_FILES['userfile']['name']= $files['userfile']['name'][$i]; $_FILES['userfile']['type']= $files['userfile']['type'][$i]; $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; $_FILES['userfile']['error']= $files['userfile']['error'][$i]; $_FILES['userfile']['size']= $files['userfile']['size'][$i]; $this->upload->initialize($this->set_upload_options()); $this->upload->do_upload(); } } private function set_upload_options() { //upload an image options $config = array(); $config['upload_path'] = './Images/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '0'; $config['overwrite'] = FALSE; return $config; }
Thank you guys!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With