I simply can't upload multiple files using PHP Codeigniter. It's uploading the first image I selected,but for the rest, it results with an offset error.
Can anyone enlighten me,what am I doing wrong?
View file:
<form method="POST" action="<?php echo base_url('upload/dosya_yukle'); ?>" enctype="multipart/form-data">
<input type="file" name="dosya[]" multiple>
<br><br>
<button type="submit" name="submit">Yükle</button>
</form>
Controller method:
public function dosya_yukle () {
$count = count($_FILES['dosya']['name']);
for ($i=0; $i < $count; $i++) {
$_FILES['dosya']['name'] = $_FILES['dosya']['name'][$i];
$_FILES['dosya']['type'] = $_FILES['dosya']['type'][$i];
$_FILES['dosya']['tmp_name'] = $_FILES['dosya']['tmp_name'][$i];
$_FILES['dosya']['error'] = $_FILES['dosya']['error'][$i];
$_FILES['dosya']['size'] = $_FILES['dosya']['size'][$i];
$config['upload_path'] = './assets/img';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->do_upload('dosya');
}
}
Error I get :
Severity: Notice
Message: Uninitialized string offset: 2
Filename: controllers/Upload.php
Line Number: 54
Backtrace:
File: C:\wamp64\www\ajquery\application\controllers\Upload.php Line: 54 Function: _error_handler
File: C:\wamp64\www\ajquery\index.php Line: 315 Function: require_once
Update controller as follows..
public function dosya_yukle () {
$count = count($_FILES['dosya']['name']);
$files = $_FILES;
unset($_FILES);
for ($i=0; $i < $count; $i++) {
$_FILES['dosya']['name'] = $files['dosya']['name'][$i];
$_FILES['dosya']['type'] = $files['dosya']['type'][$i];
$_FILES['dosya']['tmp_name'] = $files['dosya']['tmp_name'][$i];
$_FILES['dosya']['error'] = $files['dosya']['error'][$i];
$_FILES['dosya']['size'] = $files['dosya']['size'][$i];
$config['upload_path'] = './assets/img';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->do_upload('dosya');
}
}