Skip to content

How To Upload Image Using Postman In Codeigniter

API Testing Blog

Uploading Images with Postman in CodeIgniter: A Comprehensive Guide

This guide will walk you through the process of uploading images to your CodeIgniter application using Postman, providing detailed steps, practical examples, and sample code to ensure a seamless and efficient testing experience.

1. Setting up the CodeIgniter Controller and Model

First, you need to create a controller and model in your CodeIgniter project to handle the image upload process.

Controller: uploads.php:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Uploads extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Upload_model');
}
public function index() {
// Handle the image upload
if ($this->input->post()) {
$config['upload_path'] = './uploads/'; // Destination folder
$config['allowed_types'] = 'gif|jpg|png'; // Allowed file types
$config['max_size'] = '1000'; // Max file size (KB)
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) { // 'userfile' is the field name in your form
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
} else {
$upload_data = $this->upload->data();
$this->Upload_model->save_image($upload_data['file_name']); // Save image details to database
$this->load->view('upload_success');
}
} else {
$this->load->view('upload_form');
}
}
}

Model: upload_model.php:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Upload_model extends CI_Model {
public function save_image($image_name) {
$data = array(
'image_name' => $image_name,
// Add other fields as necessary (e.g., user_id, description)
);
$this->db->insert('images', $data);
return $this->db->insert_id();
}
}

2. Configuring Postman for Image Upload Testing

Now, let’s set up Postman to send image data to your CodeIgniter API.

1. Create a new Postman request:

  • Select POST as the HTTP method.
  • Enter the URL of your CodeIgniter API endpoint (e.g., http://localhost/your-project/uploads)

2. Set Headers:

  • Content-Type: multipart/form-data

3. Add a Body section:

  • Choose form-data as the body type.
  • Click Add File and select the image you want to upload.
  • Key: This should match the field name used in your CodeIgniter controller (e.g., userfile).
  • Value: The image file itself.

4. Send the request:

  • Click the Send button to submit the request.

Here’s a practical example:

Image of Postman request with image upload

3. Debugging and Troubleshooting

If your image upload fails, use Postman’s Response tab to debug the issue. Check for any error messages that may indicate problems with the API response.

  • Error codes: Look for error codes returned by your CodeIgniter API (e.g., 400 - Bad Request, 404 - Not Found, 500 - Internal Server Error).
  • Validation errors: Verify that the image file meets your defined restrictions (file type, size).
  • Server logs: Review your server logs for any clues about the upload failure.

4. Handling Multiple Images

You can extend the above code to handle multiple image uploads using a loop in your CodeIgniter controller.

Controller:

<?php
// ...
foreach ($_FILES['userfile']['name'] as $key => $value) {
$_FILES['userfile']['name'][$key] = $value;
$_FILES['userfile']['type'][$key] = $_FILES['userfile']['type'][$key];
$_FILES['userfile']['tmp_name'][$key] = $_FILES['userfile']['tmp_name'][$key];
$_FILES['userfile']['error'][$key] = $_FILES['userfile']['error'][$key];
$_FILES['userfile']['size'][$key] = $_FILES['userfile']['size'][$key];
$this->upload->initialize($config);
if ($this->upload->do_upload('userfile['.$key.']')) {
// ...
} else {
// ...
}
}
// ...

5. Security Best Practices

Implement these security measures to protect your API from vulnerabilities:

  • File validation: Always validate uploaded files to prevent malicious content or oversized files from being uploaded.
  • Input sanitization: Sanitize user input to prevent cross-site scripting (XSS) attacks.
  • Secure storage: Store uploaded files in a secure location that is not accessible directly from the web.
  • Rate limiting: Implement rate limiting to prevent DoS attacks.
  • Authentication: Authenticate users to prevent unauthorized uploads.

By following these steps and incorporating security best practices, you’ll be able to confidently upload images to your CodeIgniter API using Postman, improving your API testing process and ensuring the security of your applications.

API Testing Blog