codeigniter
  1. codeigniter-upload-multiple-file-and-image-in-codeigniter

Upload Multiple File and Image in CodeIgniter

Uploading multiple files and images is a common requirement in web applications. In CodeIgniter, it can be achieved easily with the help of its built-in File Uploading class. In this tutorial, we will learn how to upload multiple files and images in CodeIgniter.

Syntax

The syntax for uploading multiple files and images in CodeIgniter is as follows:

$this->load->library('upload');
if (!empty($_FILES['userFiles']['name'])) {
    $filesCount = count($_FILES['userFiles']['name']);
    for ($i = 0; $i < $filesCount; $i++) {
        $_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i];
        $_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i];
        $_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i];
        $_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i];
        $_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i];
        $uploadPath = './uploads/';
        $config['upload_path'] = $uploadPath;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2048';
        $this->load->library('upload', $config);
        if ($this->upload->do_upload('userFile')) {
            $fileData = $this->upload->data();
            $uploadData[$i]['file_name'] = $fileData['file_name'];
            $uploadData[$i]['created'] = date("Y-m-d H:i:s");
            $uploadData[$i]['modified'] = date("Y-m-d H:i:s");
        }
    }
    if (!empty($uploadData)) {
        //Insert file information into the database
        $insert = $this->file->insert($uploadData);
    }
}

Example

The following example demonstrates how to upload multiple files and images in CodeIgniter:

<form method="post" action="" enctype="multipart/form-data">
    <input type="file" name="userFiles[]" multiple>
    <input type="submit" name="submit" value="UPLOAD">
</form>

Explanation

In the code example above, we first load the upload library and check if there are any files to upload with !empty($_FILES['userFiles']['name']). We then get the count of the number of files to upload and loop through each file using a for loop.

Inside the loop, we set the $_FILES array data for each file and configure the file upload preferences, such as upload destination, allowed file types, and maximum file size. We then load the upload library with the configuration and call the do_upload() method to upload the file.

If the file is uploaded successfully, we retrieve the file information using the data() method and store it in an array. Finally, we insert the file information into the database.

Use

Uploading multiple files and images is a common requirement in web applications. With the help of CodeIgniter's built-in File Uploading class, we can easily upload multiple files and images in a secure and efficient way.

Important Points

  • CodeIgniter's File Uploading class has built-in security features such as filename and file type checks.
  • It is important to configure the file upload preferences properly to ensure that the uploaded files meet the requirements of the application.
  • Uploading large files may require additional configuration of the PHP upload_max_filesize and post_max_size settings in the server's php.ini file.

Summary

Uploading multiple files and images in CodeIgniter is a common requirement in web applications. With the help of its built-in File Uploading class, we can easily upload multiple files and images in a secure and efficient way. It is important to configure the file upload preferences properly to ensure that the uploaded files meet the requirements of the application. Additionally, uploading large files may require additional configuration of the PHP settings in the server's php.ini file.

Published on: