Launch offer
Business website $150 USD Custom plugin $200 USD Ready in 5 days
Get a quote
CodeIgniter

Image Upload and Validation in CodeIgniter

Restrict uploads to images and validate mime type, extension, and size.

Image Upload and Validation in CodeIgniter — a practical guide to CodeIgniter image upload with clear examples you can reuse in real projects.

CodeIgniter Tutorial Series (42/74). Prefer one article? Read the complete CodeIgniter tutorial.

Short description

For images, validate type carefully and optionally resize with the Image library.

Image rules + move

$validation = ConfigServices::validation();
$validation->setRules([
    'avatar' => 'uploaded[avatar]|is_image[avatar]|max_size[avatar,2048]',
]);
if (! $validation->withRequest($this->request)->run()) {
    return redirect()->back()->withInput()->with('errors', $validation->getErrors());
}
$img = $this->request->getFile('avatar');
$img->move(FCPATH . 'uploads');

Leave a reply

Your email address will not be published. Required fields are marked *