Home > Backend Development > PHP Tutorial > How to Achieve Multiple File Uploads in CodeIgniter Using a Single Input Field?

How to Achieve Multiple File Uploads in CodeIgniter Using a Single Input Field?

Linda Hamilton
Release: 2024-11-28 19:00:12
Original
194 people have browsed it

How to Achieve Multiple File Uploads in CodeIgniter Using a Single Input Field?

Multiple File Upload in CodeIgniter with Single Input

In this article, we'll delve into a query regarding multiple file upload using a single element in CodeIgniter. By evaluating the provided form, controller, file upload method, and the encountered issue, we'll dissect and resolve this issue to provide a comprehensive solution.

The initial issue stemmed from the file upload method not aligning correctly with the incoming file input. The adjustment proposed by @Denmark aptly rectified this discrepancy by reassigning the form input name from "images" to "images[]" to match the multiple file selection.

Revised File Upload Method:

private function upload_files($path, $title, $files)
{
    $config = array(
        'upload_path'   => $path,
        'allowed_types' => 'jpg|gif|png',
        'overwrite'     => 1,                       
    );

    $this->load->library('upload', $config);

    $images = array();

    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];

        $fileName = $title . '_' . $image;

        $images[] = $fileName;

        $config['file_name'] = $fileName;

        $this->upload->initialize($config);

        if ($this->upload->do_upload('images[]')) {
            $this->upload->data();
        } else {
            return false;
        }
    }

    return $images;
}
Copy after login

By following this revised method, you can successfully upload multiple files using a single input element in CodeIgniter.

The above is the detailed content of How to Achieve Multiple File Uploads in CodeIgniter Using a Single Input Field?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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