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

Leave a Reply

Related Posts: You may like

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

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

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.