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

Author: 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.

Leave a Reply

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

Related Posts: You may like

Online Directory Software – Local Business Directories Application

Pagination in CodeIgniter With Search Query

Codeigniter pagination with search query string

Minify HTML in CodeIgniter using Hooks

Redirect http to https using htaccess

How to execute mysql query inside codeigniter helper

Upload and Resize Image and Create Thumbnail Codeigniter

Pagination In CodeIgniter With Example

Upload file with database codeigniter

Simple codeigniter login system

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Free online tool to download Instagram videos, photos and save Instagram videos, photos to your device. It's free & easy to use.

Download Now

CURRENCY CONVERTER

Our real time currency converter calculator will convert your money based on current values from around the world. It's free & easy to use.

Convert Now

ROBOST.TXT GEN.

Create robots txt file that help search engines to crawling and indexing your site with help of our free online Robots.txt Generator tool.

Create Now

HTML MINIFIER

Free online tool to minify or compress html code and reduce the size of html code in few clicks. It's free & easy to use.

Minify Now

CSS MINIFIER

Free online tool to minify or compress css code and reduce the size of css code in few clicks. It's free & easy to use.

Minify Now

JAVASCRIPT MINIFIER

Free online tool to minify or compress javascript code and reduce the size of javascript code in few clicks. It's free & easy to use.

Minify Now

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.