How to rename file on upload in codeigniter?

Today, We want to share with you rename file upload codeigniter.In this post we will show you Renaming an uploaded file in CodeIgniter, hear for codeigniter get filename before upload we will give you demo and example for implement.In this post, we will learn about Laravel Renaming Uploaded Files Automatically with an example.

Dynamically Codeigniter Image Upload Rename

Step 1.

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

Read Also:  how to insert multiple image in database using codeigniter?

Example 1: Change controller
Use below in your own controller.

class Test extends CI_Controller {

	public function index()
	{
		$this->load->library('upload', $config); 
		if($this->input->post('uploadImage'))
		{
			$image = time().'-'.$_FILES["image"]['name']; //Filename
			$config = array(
						'upload_path' => "./uploads/",   //here folder name added Upload file Destination 
						'allowed_types' => "gif|jpg|png|jpeg|JPEG|JPG|PNG|GIF",  //some added Accepted types
						'overwrite' => TRUE,
						'max_size' => "1024",    //here set your Maximum upload file size
						'max_height' => "985",
						'max_width' => "1600",
						'file_name' => $image
					);
			
			if($this->upload->do_upload('image'))
			{
				$result['msg']="Good Luck, File uploaded successfully!";
			} else {
				$result['msg']="Sorry, Error in upload file.";
			}
			$this->load->view('image', $result);
		}
	}
}

encrypt file name with use of CI native option:

$config['encrypt_name'] = TRUE;

your own source code:

$new_name = time().$_FILES["userfiles"]['name'];
$config['file_name'] = $new_name;

I hope you get an idea about Rename file upload codeigniter.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.