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

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

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

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.