Home > Backend Development > PHP Tutorial > Why Am I Getting \'You Did Not Select a File to Upload\' When Uploading Multiple Files in CodeIgniter?

Why Am I Getting \'You Did Not Select a File to Upload\' When Uploading Multiple Files in CodeIgniter?

Linda Hamilton
Release: 2024-11-27 15:29:13
Original
863 people have browsed it

Why Am I Getting

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;
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template