Rename file while uploading in codeigniter

Rename file while uploading in codeigniter

Today i am sharing quite interest tutorial about how to rename or change the file name of uploaded files with CodeIgniter.
If you trying to add time as the prefix of the image name along with the original name when uploading, But you couldn’t figure it out. Then this tutorial perfect for you.

Step 1. Create controller for “Rename file while uploading in codeigniter”

Create a controller called Image.php. in it copy and past below code and save it to your application/controllers/ directory:

<?php
/*
	Author: Mukesh Jakhar
	Description: Rename file while uploading in codeigniter (Controller)
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Image extends CI_Controller {
	function __construct() {
		parent::__construct();
	}

	public function index()
	{
		if($this->input->post('uploadImage'))
		{
			$image = time().'-'.$_FILES["image"]['name'];

			$config = array(
			'upload_path' => "./uploads/",
			'allowed_types' => "gif|jpg|png|jpeg|JPEG|JPG|PNG|GIF",
			'overwrite' => TRUE,
			'max_size' => "99999999999",
			'max_height' => "800",
			'max_width' => "1500",
			'file_name' => $image
			);
			$this->load->library('upload', $config); 
			
			if($this->upload->do_upload('image'))
			{
				$res['msg']="Image has been uploaded!";
				$this->load->view('image', $res);
			}
			else
			{
				$this->load->view('image');
			}
		}
	}
}

Step 2. Create view for “Rename file while uploading in codeigniter”

Create a view called image.php. in it copy and past below code and save it to your application/views/ directory:

<?php
/*
	Author: Mukesh Jakhar
	Description: Rename file while uploading in codeigniter (View)
*/
defined('BASEPATH') OR exit('No direct script access allowed');
$this->load->helper('url');
?>
<!DOCTYPE HTML>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>User registration form</title>
	<meta name="viewport" content="width=device-width, initial-scale=1" />
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
</head>
<body>
<div class="container">
	<div class="col-md-3"></div>
	<div class="col-md-6">
		<?php if(isset($msg)){echo $msg;}?>
		<form method="post" enctype="multipart/form-data">
			<div class="form-group">
				<label>Upload File</label>
				<input class="form-control" type="file" name="image" />
			</div>
			<div class="form-group">
				<input class="btn btn-primary" type="submit" value="Upload File" />
			</div>
		</form>
	</div>
	<div class="col-md-3"></div>
</div>
</body>
</html>

Tags: Codeigniter Rename file on upload, CodeIgniter How to change uploaded file name, How to rename an uploaded image in Codeigniter, codeigniter file upload example, codeigniter get filename before upload, edit uploaded file in codeigniter, image upload in codeigniter with database example, upload pdf file in codeigniter, how to upload, rename, save image to database in php codeigniter, Rename file upload codeigniter, Dynamically Codeigniter Image Upload Rename, Renaming an uploaded file in CodeIgniter

Mukesh Jakhar - PHP Web Developer
Mukesh Jakhar

This is Mukesh Jakhar. I’m a professional web developer with 3+ years experience. I am always ready for freelance work and i am writing blogs in my free time. I love to learn new technologies and share with others.

I founded PHPKIDA in September 2015. The focus of this website to provide web development tutorials of PHP, WordPress, CodeIgniter, Jquery, MySQL, HMTL, CSS etc and sharing solution to problems which i already solved.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.