Multiple File Uploads with CodeIgniter
Issue:
When attempting to upload multiple files using the provided example, the error message "You did not select a file to upload." persistently appears.
Solution:
The issue lies within the upload method. To correctly upload multiple files using CodeIgniter, the code should be updated as suggested below:
private function upload_files($title, $files) { $config = array( 'upload_path' => './upload/real_estate/', 'allowed_types' => 'jpg|gif|png', 'overwrite' => 1, ); $this->load->library('upload', $config); foreach ($files['name'] as $key => $image) { $_FILES['images[]']['name']= $files['name'][$key]; $_FILES['images[]']['type']= $files['type'][$key]; $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key]; $_FILES['images[]']['error']= $files['error'][$key]; $_FILES['images[]']['size']= $files['size'][$key]; $config['file_name'] = $title .'_'. $image; $this->upload->initialize($config); if ($this->upload->do_upload('images[]')) { $this->upload->data(); } else { return false; } } return true; }
Explanation:
The primary difference lies in the initialization of the upload method. When using multiple file inputs with the same name, the input array must be referenced as '$_FILES['images[]']', instead of '$_FILES['images']', as seen in the corrected code provided. This ensures that each individual file is processed and uploaded correctly.
The above is the detailed content of Why Am I Getting \'You Did Not Select a File to Upload\' When Uploading Multiple Files in CodeIgniter?. For more information, please follow other related articles on the PHP Chinese website!